Answers for "how to limit decimal in double kotlin"

1

how to limit decimal in double kotlin

val number:Double = 0.0449999
val number3digits:Double = String.format("%.3f", number).toDouble()
val number2digits:Double = String.format("%.2f", number3digits).toDouble()
val solution:Double = String.format("%.1f", number2digits).toDouble()

//Or add extension 
fun Double.shorten(decimals: Int): Double{ return String.format("%.$decimals" + "f", this).replace(',', '.').toDouble() }
Posted by: Guest on February-21-2021

Code answers related to "how to limit decimal in double kotlin"

Browse Popular Code Answers by Language