# Multiplatform BigDecimal implementation?

**URL:** <https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631>\
**Category:** Libraries\
**Created:** [November 30, 2017, 11:09pm UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631 "2017-11-30T23:09:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bissell](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bissell/32/4483_2.png) [@bissell](https://discuss.kotlinlang.org/u/bissell)\
**Post date:** [November 30, 2017, 11:09pm UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/1 "2017-11-30T23:09:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [December 1, 2017, 5:59am UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/2 "2017-12-01T05:59:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jonathan.Haas](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jonathan.Haas](https://discuss.kotlinlang.org/u/Jonathan.Haas)\
**Post date:** [December 4, 2017, 6:58am UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/3 "2017-12-04T06:58:51Z")

</div>

Probably could do it right now: [bigdecimal - npm](https://www.npmjs.com/package/bigdecimal)

---

<div class="post-metadata">

**Author:** ![davidvavra](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/davidvavra/32/4729_2.png) [@davidvavra](https://discuss.kotlinlang.org/u/davidvavra)\
**Post date:** [February 1, 2018, 5:34pm UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/4 "2018-02-01T17:34:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bissell](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bissell/32/4483_2.png) [@bissell](https://discuss.kotlinlang.org/u/bissell)\
**Post date:** [May 20, 2018, 12:33am UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/5 "2018-05-20T00:33:56Z")

</div>

I’ve set up a first cut at a library at [kmulti-bignumber](https://github.com/kmulti/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. 🙂

---

<div class="post-metadata">

**Author:** ![ionspin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ionspin/32/6177_2.png) [@ionspin](https://discuss.kotlinlang.org/u/ionspin)\
**Post date:** [March 19, 2019, 9:32am UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/6 "2019-03-19T09:32:31Z")

</div>

Here’s my attempt at bignum library [kotlin-multiplatform-bignum](https://github.com/ionspin/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!

---

<div class="post-metadata">

**Author:** ![ionspin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ionspin/32/6177_2.png) [@ionspin](https://discuss.kotlinlang.org/u/ionspin)\
**Post date:** [March 30, 2019, 10:15pm UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/7 "2019-03-30T22:15:55Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![skolson5903](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@skolson5903](https://discuss.kotlinlang.org/u/skolson5903)\
**Post date:** [July 31, 2020, 9:21pm UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/8 "2020-07-31T21:21:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![skolson5903](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@skolson5903](https://discuss.kotlinlang.org/u/skolson5903)\
**Post date:** [July 31, 2020, 10:01pm UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/9 "2020-07-31T22:01:55Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![kendy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kendy/32/8466_2.png) [@kendy](https://discuss.kotlinlang.org/u/kendy)\
**Post date:** [April 4, 2021, 7:51pm UTC](https://discuss.kotlinlang.org/t/multiplatform-bigdecimal-implementation/5631/10 "2021-04-04T19:51:23Z")

</div>

Recently I was searching for BigDecimal for iOS. I found no drop-in replacement, so I’ve ported the BigDecimal from the Android Open Source Project to Kotlin/Native: [GitHub - kendy/Kotlin-Native-BigDecimal: Kotlin/Native BigDecimal implementation](https://github.com/kendy/Kotlin-Native-BigDecimal) Hope you’ll find it useful 🙂
