From 27dd8c98cc4610e33793ec7de53cf13afbf8e8e8 Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 15 Jul 2020 19:40:04 +0200 Subject: [PATCH] Added parameter countDecimalPlaces to format() as is not generic on all platforms (e.g. doesn't work on iOS) --- .../kotlin/net/dankito/utils/multiplatform/BigDecimal.kt | 2 +- .../kotlin/net/dankito/utils/multiplatform/BigDecimal.kt | 6 +++--- .../kotlin/net/dankito/utils/multiplatform/BigDecimal.kt | 4 ++-- .../net/dankito/banking/ui/presenter/BankingPresenter.kt | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt index 6e7432ad..f68cedb5 100644 --- a/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt +++ b/common/src/commonMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt @@ -16,6 +16,6 @@ expect class BigDecimal { constructor(double: Double) - fun format(pattern: String): String + fun format(countDecimalPlaces: Int): String } \ No newline at end of file diff --git a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt index bd46832c..ad8b066a 100644 --- a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt +++ b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt @@ -23,11 +23,11 @@ actual class BigDecimal(val decimal: NSDecimalNumber) { // it's almost impossibl actual constructor(decimal: String) : this(decimal.toDouble()) - actual fun format(pattern: String): String { + actual fun format(countDecimalPlaces: Int): String { val formatter = NSNumberFormatter() - formatter.positiveFormat = pattern - formatter.negativeFormat = pattern + formatter.minimumFractionDigits = countDecimalPlaces.toULong() + formatter.maximumFractionDigits = countDecimalPlaces.toULong() return formatter.stringFromNumber(this.decimal) ?: "" } diff --git a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt index b4468d4e..99400a31 100644 --- a/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt +++ b/common/src/jvmMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt @@ -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 fun format(pattern: String): String { - return String.format(pattern, this) + actual fun format(countDecimalPlaces: Int): String { + return String.format("%.0${countDecimalPlaces}f", this) } diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt index 927de65d..bdd0ddc2 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt @@ -341,7 +341,7 @@ open class BankingPresenter( } open fun formatAmount(amount: BigDecimal): String { - return amount.format("%.02f") + return amount.format(2) }