Is there any chance for Multiplatform version of java’s BigDecimal?
I know that there are 3rd party libraries like kotlin-multiplatform-bignum, but there aren’t as easy to use as built in types. If Multiplatform stdlib included this as normal numeric type this would be very easy to use, eg with B suffix
val number1 = 1_000_000L // Long
val number2 = 1_000_000B // BigDecimal
I’m guessing the main issue is that literals above the Int or Long limit won’t be convertible to BigDecimal. Also, having one accepted solution made by the language is very useful, especially when it’s something as lite as a BigDecimal implementation. It’s kind of in the same alley as having a Duration type for example.
For jvm you may also try Deci - it wraps BigDecimal for easier usage in math expressions
If all source types are Deci: val d1: Deci = (price * quantity - fee) * 100 / (price * quantity) round 2
If source numbers are Int or BigDecimal, they can be converted to Deci with “.deci” suffix: val d2: BigDecimal = ((1.deci - 1.deci / 365) * (1.deci - 2.deci / 365) round 11).toBigDecimal()
Since the topic is reanimated, there is some contribution activity on multiplatform BigInteger in KMath: UBigInt · Issue #340 · SciProgCentre/kmath · GitHub. The performance is similar to Java BigInt already. We still lack BigDecimal, but feel free to file a feature request. The discussion is in mathematics channel in kotlin slack.
If you need a drop-in replacement of Java BigDecimal, I’ve ported the Android implementation to Kotlin, usable for Kotlin/Native (particularly for iOS). It cannot support Kotlin/JS though, as it depends on BoringSSL for the actual BIGNUM implementation: GitHub - kendy/Kotlin-Native-BigDecimal: Kotlin/Native BigDecimal implementation
Having said that, in the 2nd part of your question, you ask for native language support; so actually it is unclear to me what do you want to achieve? What is the problem you want to solve?
What are specific difficulties arise when using BigDecimal, besides the absence of literals?
BigDecimal is available only in JVM, so it can’t be used in common code. I thought about wrapping BigDecimal on JVM and other platform-specific implementations with common type using expect/actual, but it wouldn’t guarantee consistent behavior between platforms (such as number of decimals after dividing 1 by 3)