Kotlin:
Rounding numbers
How to:
In Kotlin, rounding can be done using several functions like roundToInt()
, roundToDouble()
, and using BigDecimal
for more control:
fun main() {
val number1 = 3.14159
println(number1.roundToInt()) // Outputs: 3
val number2 = 3.5
println(number2.roundToInt()) // Outputs: 4
val number3 = 123.456
println("%.2f".format(number3)) // Outputs: 123.46
val bigDecimal = number3.toBigDecimal().setScale(1, RoundingMode.HALF_EVEN)
println(bigDecimal) // Outputs: 123.5
}
Deep Dive
Historically, rounding numbers has been a fundamental concept in both mathematics and computation, designed to handle numerical precision limitations. In early computing, rounding was critical due to the high cost of memory.
In Kotlin, rounding is built upon the standard Java libraries. Options for rounding include Math.round()
, which rounds to the nearest whole number, and BigDecimal
for customizable rounding, where you can specify a scale and a RoundingMode
.
Each RoundingMode
has different policies for handling ties (when the digit is exactly in the middle of the options for rounding). For instance, RoundingMode.HALF_UP
rounds to the nearest neighbor, unless both neighbors are equidistant, in which case it rounds up.
See Also
- Kotlin Documentation on
BigDecimal
- Oracle’s Java Documentation for
RoundingMode
- IEEE Standard for Floating-Point Arithmetic (IEEE 754) IEEE Standard 754