How do you round a number to N decimal places


#1

So I saw this post about my problem(Print floats with certain amount of decimal numbers)

And I was wondering I it possible to use a methood or something else rather “%.2f”.format(value) in order to achive the same thing


#2

You should explain better you problem.

Technically you cannot round the IEEE 754 floating point 0.1 to the first decimal point.

However you can use

fun Double.round(decimals: Int): Double {
    var multiplier = 1.0
    repeat(decimals) { multiplier *= 10 }
    return round(this * multiplier) / multiplier
}

#3

I’m just trying to short the number to lets say 2 decimal places.

for example I want to turn:
pi=3.14159265359
to just
pi=3.14

I hope you can understand me know, or at least more clear to understand.