Multiplatform BigDecimal implementation?

I’m starting work on a new multiplatform Kotlin project and quickly ran into a snag in trying to use BigDecimal in a platform agnostic way. It’s looking like there will be a lot of work in translating java.math.BigDecimal into a multiplatform expect class, and then providing the JS (and maybe someday down the road, native) implementation.

Was thinking I might just create my own library for it, but does anyone know if this has already been done?


I was thinking about the same thing for Native. So, good idea.

Probably could do it right now:

This is a biggest blocker for me to use Kotlin Multiplatform. A library would be welcome. Maybe I can contribute.

I’ve set up a first cut at a library at kmulti-bignumber. It has an expect declaration for BigDecimal with some of the essential methods and operators. For the JVM it just directly uses typealias from java.math.BigDecimal. I haven’t done anything on JS or BigInteger yet and probably won’t for awhile, so please send PRs. :slight_smile:

Here’s my attempt at bignum library kotlin-multiplatform-bignum . I choose a different approach, by starting with a pure kotlin-common implementation. Currently it only has integer support so it doesn’t solve your problem just yet, but I’ll be working on floating point soon. It’s currently slow, no Karatsuba, Toom-Cook, etc., but basic integer operations should be working. PRs and advice are welcome!


Just a quick update, I’ve added support for BigDecimal, should be working fine, albeit slow as I haven’t yet implemented faster algorithms for multiplication and division such as Karatsuba, Newtons method for reciprocal, etc. That’s next on my list. Again, PRs and advice welcome!


I’m trying out BigDecimal in multi-platform, and I don’t see anything like toInt(), toLong(), toDouble(), toFloat(). Anybody know if this functionality is in there somewhere, or do I need to do extensions?

Hmmm, digging in the code it looks like I’m wanting BigDecimal to fully implement the NarrowingOperations interface and it doesn’t. So extension functions here I come :slight_smile: