How to round double input to two decimal places? G...
# mathematics
z
How to round double input to two decimal places? Given the output is Decimal something like
toFixed(2)
in JS
i
As @Iaroslav Postovalov already mentioned, this will work for you
Copy code
private val MY_FORMAT = DecimalFormat("0.##")
val x = 4.378 println(MY_FORMAT.format(x)) // 4.38
a
Last time I checked, DecimalFormat was available only for JVM
In JS you have to use appropriate Js library or regexp to cut the tail of the string.
i
Actually, not many people really use Kotlin/Multiplatform without denoting it explicitly, so I usually presume that topicstarter is targeting JVM.
And actually @zain themself mentioned that on JS it is done with
Number.prototype.toFixed
...
👍 1