Round up a float or a double with 2 decimal places in Kotlin
This article explores different ways to round up a float or a double with 2 decimal places in Kotlin.
1. Using roundToInt() function
The roundToInt() function rounds a double value to the nearest integer. It can be used as follows to round up a float or double with the required decimal places.
|
1 2 3 4 5 6 7 8 9 |
import kotlin.math.roundToInt fun main() { val random = 0.8458215996440445 val roundoff = (random * 100.0).roundToInt() / 100.0 println(roundoff) // 0.85 } |
The number of 0’s indicates the number of decimal places in the output. Therefore, to round with 4 decimal places, use value 10000.0:
|
1 2 3 4 5 6 7 8 9 |
import kotlin.math.roundToInt fun main() { val random = 0.037854093052263726 val roundoff = (random * 10000.0).roundToInt() / 10000.0 println(roundoff) // 0.0379 } |
Also, consider this alternative and equivalent version of the above code:
|
1 2 3 4 5 6 7 8 9 |
import kotlin.math.roundToInt fun main() { val random = 0.797490220519589 val roundoff = (random * 10000).roundToInt().toDouble() / 10000 println(roundoff) // 0.7975 } |
It’s worth noting that the floating-point arithmetic is very tricky, and might not always work as expected. For example, the value 295.335 gets rounded “down” to 295.33 instead of rounding “up” to 295.34.
|
1 2 3 4 5 6 7 8 |
import kotlin.math.roundToInt fun main() { val random = 295.335 println(random * 100.0) // 29533.499999999996 println((random * 100.0).roundToInt() / 100.0) // 295.33 } |
2. Using DecimalFormat.format() function
Alternatively, we can call the DecimalFormat.format() function to restrict the double to 2-decimal points using the pattern #.##. The RoundingMode can be provided using the setRoundingMode() function.
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.math.RoundingMode import java.text.DecimalFormat fun main() { val random = 8435.21057752090819915 val df = DecimalFormat("#.##") df.roundingMode = RoundingMode.DOWN val roundoff = df.format(random) println(roundoff) // 8435.21 } |
Note that the number of # after the dot indicates the number of decimal places. Therefore, to round with 3 decimal places, use the pattern #.###:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.math.RoundingMode import java.text.DecimalFormat fun main() { val random = 4732.8326486752163523 val df = DecimalFormat("#.###") df.roundingMode = RoundingMode.DOWN val roundoff = df.format(random) println(roundoff) // 4732.832 } |
This solution faces the same issue as the roundToInt() function if no rounding mode is provided. i.e, the value 295.335 gets rounded “down” to 295.33 instead of rounding “up” to 295.34.
|
1 2 3 4 5 6 7 8 9 |
import java.text.DecimalFormat fun main() { val random = 295.335 val df = DecimalFormat("#.##") val roundoff = df.format(random) println(roundoff) // 295.33 } |
3. Using String.format() function
We can also use the String.format() function to round up a float or a double with the specific number of decimal places. This works fine for the value 295.335, as shown below:
|
1 2 3 4 5 |
fun main() { val random = 295.335 val roundoff = String.format("%.2f", random) println(roundoff) // 295.34 } |
4. Using BigDecimal
Finally, we can convert the double value to a BigDecimal and restrict the double to 2-decimal points using the setScale() function with the specified RoundingMode.
|
1 2 3 4 5 6 7 8 9 |
import java.math.BigDecimal import java.math.RoundingMode fun main() { val random = Math.random() val bd = BigDecimal(random) val roundoff = bd.setScale(2, RoundingMode.FLOOR) println(roundoff) } |
That’s all about rounding up a float or a double with 2 decimal places in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)