# Decimal format in a multi-platform build

**URL:** <https://discuss.kotlinlang.org/t/decimal-format-in-a-multi-platform-build/5692>\
**Category:** Libraries\
**Created:** [December 6, 2017, 6:57am UTC](https://discuss.kotlinlang.org/t/decimal-format-in-a-multi-platform-build/5692 "2017-12-06T06:57:18Z")\
**Posts on this page:** 6\
**Page:** 1

<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 6, 2017, 6:57am UTC](https://discuss.kotlinlang.org/t/decimal-format-in-a-multi-platform-build/5692/1 "2017-12-06T06:57:18Z")

</div>

I am trying to converting one of my JavaFX projects to a multi-platform one. Since I do not have any experience with javascript ecosystem it is moving very slow.

One specific problem I encountered, that there seems to be no way to define a format for numbers in common library. All I need is to limit floating point precision in the output to 2 digits. The problem is that both `common` and `js` do not have either `String.fotmat` or `DecimalFormat`, so I am not sure how to do it. For now, I declared an expected tooling function, which in JS returns number as-is, but it is not a solution.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![ilogico](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilogico/32/3725_2.png) [@ilogico](https://discuss.kotlinlang.org/u/ilogico)\
**Post date:** [December 6, 2017, 7:55am UTC](https://discuss.kotlinlang.org/t/decimal-format-in-a-multi-platform-build/5692/2 "2017-12-06T07:55:21Z")

</div>

In Javascript, you have: `Number.toFixed(decimalDigits: Int): String`  
You also have de Intl API that allows you to format numbers in different ways and for different cultures.

---

<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 6, 2017, 8:01am UTC](https://discuss.kotlinlang.org/t/decimal-format-in-a-multi-platform-build/5692/3 "2017-12-06T08:01:54Z")

</div>

Thanks for suggestion. In fact it should be `num.asDynamic().toFixed(digits) as String` (found the solution [here](https://stackoverflow.com/questions/42791492/kotlin-js-define-number-of-decimals)).

I think it should be added to kotlin-common standard library.

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [December 6, 2017, 8:45am UTC](https://discuss.kotlinlang.org/t/decimal-format-in-a-multi-platform-build/5692/4 "2017-12-06T08:45:13Z")

</div>

[https://youtrack.jetbrains.com/issue/KT-21644](https://youtrack.jetbrains.com/issue/KT-21644)

---

<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 6, 2017, 8:57am UTC](https://discuss.kotlinlang.org/t/decimal-format-in-a-multi-platform-build/5692/5 "2017-12-06T08:57:44Z")

</div>

Thanks, I’ve missed that. Though it is really hard to find something on youtrack if you do not know it is there.

---

<div class="post-metadata">

**Author:** ![real.sergeych](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/real.sergeych/32/7993_2.png) [@real.sergeych](https://discuss.kotlinlang.org/u/real.sergeych)\
**Post date:** [January 24, 2022, 2:21am UTC](https://discuss.kotlinlang.org/t/decimal-format-in-a-multi-platform-build/5692/6 "2022-01-24T02:21:00Z")

</div>

I decided to reimplement String.format (or sprintf) as MP library, connectable via dependency that works the same everywhere. It already works with numbers, while it is still early, I do use it in some of our MP projects: [GitHub - sergeych/mp\_stools: Collection of multiplatofrm string tools that does not exist on all platforms with the same interface: string and datetime formatting, etc. USeful fo write MP libraries and applications](https://github.com/sergeych/mp_stools)
