Added parameter countDecimalPlaces to format() as is not generic on all platforms (e.g. doesn't work on iOS)

This commit is contained in:
dankito 2020-07-15 19:40:04 +02:00
parent a101a37db2
commit 27dd8c98cc
4 changed files with 7 additions and 7 deletions

View File

@ -16,6 +16,6 @@ expect class BigDecimal {
constructor(double: Double) constructor(double: Double)
fun format(pattern: String): String fun format(countDecimalPlaces: Int): String
} }

View File

@ -23,11 +23,11 @@ actual class BigDecimal(val decimal: NSDecimalNumber) { // it's almost impossibl
actual constructor(decimal: String) : this(decimal.toDouble()) actual constructor(decimal: String) : this(decimal.toDouble())
actual fun format(pattern: String): String { actual fun format(countDecimalPlaces: Int): String {
val formatter = NSNumberFormatter() val formatter = NSNumberFormatter()
formatter.positiveFormat = pattern formatter.minimumFractionDigits = countDecimalPlaces.toULong()
formatter.negativeFormat = pattern formatter.maximumFractionDigits = countDecimalPlaces.toULong()
return formatter.stringFromNumber(this.decimal) ?: "" return formatter.stringFromNumber(this.decimal) ?: ""
} }

View File

@ -23,8 +23,8 @@ actual class BigDecimal actual constructor(decimal: String) : java.math.BigDecim
actual constructor(double: Double) : this(java.math.BigDecimal.valueOf(double).toPlainString()) // for object deserializers actual constructor(double: Double) : this(java.math.BigDecimal.valueOf(double).toPlainString()) // for object deserializers
actual fun format(pattern: String): String { actual fun format(countDecimalPlaces: Int): String {
return String.format(pattern, this) return String.format("%.0${countDecimalPlaces}f", this)
} }

View File

@ -341,7 +341,7 @@ open class BankingPresenter(
} }
open fun formatAmount(amount: BigDecimal): String { open fun formatAmount(amount: BigDecimal): String {
return amount.format("%.02f") return amount.format(2)
} }