roundTo

fun Double.roundTo(decimals: Int): Double

Returns this value rounded to decimals fractional digits.

Rounding mode is half-up with ties rounded away from zero, so 2.5.roundTo(0) is 3.0 and (-2.5).roundTo(0) is -3.0. Results that round to zero return positive zero. The result is the nearest representable Double, so tiny binary representation error can remain.

Throws IllegalArgumentException when decimals is outside 0..15. NaN and infinities are returned unchanged. Values whose magnitude is at least 2^52, or whose scaled magnitude exceeds Long.MAX_VALUE, are returned unchanged because they carry no meaningful digits at the requested precision.


fun Float.roundTo(decimals: Int): Float

Returns this value rounded to decimals fractional digits.

Converts to Double, applies roundTo with half-up rounding (ties away from zero), and converts back. Throws IllegalArgumentException when decimals is outside 0..15. NaN and infinities are returned unchanged.