From 2027092ff95d6eef0402ea44016de70c6a6ea0b1 Mon Sep 17 00:00:00 2001 From: dankito Date: Sun, 12 Jul 2020 17:55:15 +0200 Subject: [PATCH] Keeping NSDecimalNumber now as a class property instead of deriving from it as it's almost impossible to correctly derive from it (NSCoder seams to be the only allowed construction but crashes then at run time) --- .../dankito/utils/multiplatform/BigDecimal.kt | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) 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 b33f34d8..bd46832c 100644 --- a/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt +++ b/common/src/iosMain/kotlin/net/dankito/utils/multiplatform/BigDecimal.kt @@ -1,9 +1,6 @@ package net.dankito.utils.multiplatform -import platform.Foundation.NSCoder -import platform.Foundation.NSDecimalNumber -import platform.Foundation.NSNumberFormatter -import platform.Foundation.NSOrderedSame +import platform.Foundation.* fun NSDecimalNumber.toBigDecimal(): BigDecimal { @@ -11,23 +8,19 @@ fun NSDecimalNumber.toBigDecimal(): BigDecimal { } actual fun Collection.sum(): BigDecimal { - return this.fold(NSDecimalNumber.zero) { acc, e -> acc.decimalNumberByAdding(e) }.toBigDecimal() + return this.fold(NSDecimalNumber.zero) { acc, e -> acc.decimalNumberByAdding(e.decimal) }.toBigDecimal() } -actual class BigDecimal actual constructor(decimal: String) : NSDecimalNumber(NSCoder()) { +actual class BigDecimal(val decimal: NSDecimalNumber) { // it's almost impossible to derive from NSDecimalNumber so i keep it as property actual companion object { - actual val Zero = BigDecimal("0") // TODO: is this correct? + actual val Zero = BigDecimal(0.0) } + actual constructor(double: Double) : this(NSDecimalNumber(double)) - actual constructor(double: Double) : this(NSDecimalNumber(double).stringValue) - - - init { - super.decimalNumberByAdding(NSDecimalNumber(decimal)) - } + actual constructor(decimal: String) : this(decimal.toDouble()) actual fun format(pattern: String): String { @@ -36,16 +29,16 @@ actual class BigDecimal actual constructor(decimal: String) : NSDecimalNumber(NS formatter.positiveFormat = pattern formatter.negativeFormat = pattern - return formatter.stringFromNumber(this) ?: "" + return formatter.stringFromNumber(this.decimal) ?: "" } - override fun isEqual(`object`: Any?): Boolean { - if (`object` is BigDecimal) { - return this.compare(`object`) == NSOrderedSame + override fun equals(other: Any?): Boolean { + if (other is BigDecimal) { + return this.decimal.compare(other.decimal) == NSOrderedSame } - return super.equals(`object`) + return super.equals(other) } } \ No newline at end of file