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?
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.
Thanks for suggestion. In fact it should be num.asDynamic().toFixed(digits) as String
(found the solution here).
I think it should be added to kotlin-common standard library.
Thanks, I’ve missed that. Though it is really hard to find something on youtrack if you do not know it is there.
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