I would like to round Double variables to the next lower 0.5 value. For instance
12.03 -> 12
12.44 -> 12
12.56 -> 12.5
12.99 -> 12.5
There should be an elegant easy Kotlin like way?
I would like to round Double variables to the next lower 0.5 value. For instance
12.03 -> 12
12.44 -> 12
12.56 -> 12.5
12.99 -> 12.5
There should be an elegant easy Kotlin like way?
Multiply by 2, take the floor
, then divide by 2 again.
There doesn't seem to be a built-in way, but you can always write it yourself:
fun Double.roundDownToMultipleOf(base: Double): Double = base * floor(this / base)
Use as 12.56.roundDownToMultipleOf(0.5)
.