Finally I did what Andy Turner suggested, rounded to 3 decimals, then to 2 and then to 1:

Answer 1:

val number:Double = 0.0449999
val number3digits:Double = String.format("%.3f", number).toDouble()
val number2digits:Double = String.format("%.2f", number3digits).toDouble()
val solution:Double = String.format("%.1f", number2digits).toDouble()

Answer 2:

val number:Double = 0.0449999
val number3digits:Double = Math.round(number * 1000.0) / 1000.0
val number2digits:Double = Math.round(number3digits * 100.0) / 100.0
val solution:Double = Math.round(number2digits * 10.0) / 10.0

Result:

0.045 → 0.05 → 0.1

Note: I know it is not how it should work but sometimes you need to round up taking into account all decimals for some special cases so maybe someone finds this useful.

Answer from Noelia on Stack Overflow
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › round.html
round | Core API – Kotlin Programming Language
import kotlin.math.* import ... · expect fun round(x: Float): Float(source) Rounds the given value x towards the closest integer with ties rounded towards even integer....
🌐
Baeldung
baeldung.com › home › kotlin › kotlin numbers › rounding numbers in kotlin
Rounding Numbers in Kotlin | Baeldung on Kotlin
April 28, 2026 - As shown, when we set the scale to 2, the number is rounded to two decimal places.
Discussions

java - Round Double to 1 decimal place kotlin: from 0.044999 to 0.1 - Stack Overflow
I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 . I am using Kotlin but the Java solution is also helpful. val number:Double = 0.0449999 I tried ge... More on stackoverflow.com
🌐 stackoverflow.com
Problem with rounding a number.
To my eyes, it is fundamentally wrong to try to "round an FP number to two decimals". It should only be a way of formatting a string that describes it. Most two-decimal numbers aren't exactly representable in FP so you shouldn't do any calculations after "rounding". That's why, as a matter of coding practice, the advice to use string formatting functions is correct. More on reddit.com
🌐 r/Kotlin
11
4
August 8, 2018
android - how to round a number in kotlin - Stack Overflow
I used String.format() to round to the third decimal place. However this didn't work and I solved it using DecimalFormat. Is there anything I implemented wrong? val value = 36.295f Timber.e("f... More on stackoverflow.com
🌐 stackoverflow.com
Rounding in Kotlin - Stack Overflow
I have Kotlin Double and I hope to return Int, rounded specially. 166.66 -> 166 166.99 -> 167 There is nothing suitable in Documentation. I need something between DOWN and CEILING Rounding in More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How to limit float to 2 decimal places in Kotlin?
To limit a float to 2 decimal places, use String.format() or BigDecimal for precision. Example: ```kotlin val formatted = String.format("%.2f", 5.876F) // Output: "5.88" ``` Alternatively, use BigDecimal: ```kotlin val limited = BigDecimal(5.876).setScale(2, RoundingMode.HALF_UP).toFloat() // Output: 5.88 ```
🌐
dhiwise.com
dhiwise.com › post › kotlin-cast-float-to-int-simple-methods-for-conversion
Dhiwise
Can you cast a float to an int?
Yes, in Kotlin, you can cast a float to an int using the toInt() function. However, this directly truncates the fractional part, effectively discarding any decimals. For example, 5.87F.toInt() will result in 5. If you require rounding instead of truncation, you should use methods like roundToInt() from the kotlin.math package.
🌐
dhiwise.com
dhiwise.com › post › kotlin-cast-float-to-int-simple-methods-for-conversion
Dhiwise
🌐
Kotlin Discussions
discuss.kotlinlang.org › support › how do you round a number to n decimal places
How do you round a number to N decimal places - Support - Kotlin Discussions
August 2, 2018 - 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 …
🌐
Programiz
programiz.com › kotlin-programming › examples › round-number-decimal
Kotlin Program to Round a Number to n Decimal Places
We declare the format using the # patterns #.###. This means, we want num upto 3 decimal places. We also set the rounding mode to Ceiling, this causes the last given place to be rounded to its next number.
🌐
Codecademy
codecademy.com › docs › kotlin › math methods › round()
Kotlin | Math Methods | round() | Codecademy
October 27, 2023 - Beginner Friendly.Beginner Friendly9 hours9 hours · round(a) a: the number (either Double or Float) to be rounded · The round() function returns the rounded number. This example uses the round() function to determine the rounded value of each ...
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round.html
round - Kotlin Programming Language
kotlin-stdlib / kotlin.math / round ... (JVM source) (JS source) (Native source) Rounds the given value x towards the closest integer with ties rounded towards even integer....
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round-to-int.html
Kotlinlang
Returns the Float value nearest to this value in direction of positive infinity. ... Raises this value to the power x. ... Raises this value to the integer power n. ... Rounds the given value x towards the closest integer with ties rounded towards even integer.
Find elsewhere
🌐
Techie Delight
techiedelight.com › home › java › round up a float or a double with 2 decimal places in kotlin
Round up a float or a double with 2 decimal places in Kotlin | Techie Delight
July 7, 2026 - We can also use the String.format() function to round up a float or a double with the specific number of decimal places.
🌐
DhiWise
dhiwise.com › post › kotlin-cast-float-to-int-simple-methods-for-conversion
What You Need to Know About Kotlin Cast Float to Int
December 3, 2024 - Unexpected Rounding: Be mindful of how rounding behaves for negative values. You can format numbers for cleaner output by converting them to integers: When working with floats, you might need discrete integer values for loops: This article covered the various techniques available in Kotlin to cast float to int.
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round-to-long.html
roundToLong - Kotlin Programming Language
August 2, 2018 - Returns the Float value nearest to this value in direction of positive infinity. ... Raises this value to the power x. ... Raises this value to the integer power n. ... Rounds the given value x towards the closest integer with ties rounded towards even integer.
🌐
Techie Delight
techiedelight.com › home › java › convert a floating-point value to nearest int in kotlin
Convert a floating-point value to nearest int in Kotlin | Techie Delight
July 7, 2026 - To round the given floating-point positive value x, you can use the expression (x + 0.5).toInt(). Similarly, to round a floating-point negative value x, you can use the expression (x - 0.5).toInt(). This is demonstrated below: ... That’s all ...
🌐
TutorialsPoint
tutorialspoint.com › article › kotlin-program-to-round-a-number-to-n-decimal-places
Kotlin Program to Round a Number to n Decimal Places
October 13, 2022 - Below is a demonstration of the same ? Suppose our input is · Input : 3.1415 · The desired output would be ? Output : 3.2 · Step 1 ? START · Step 2 ? Declare a float value namely myInput.
🌐
Codecademy
codecademy.com › docs › kotlin › math methods › ceil()
Kotlin | Math Methods | ceil() | Codecademy
October 26, 2023 - The ceil() method in Kotlin’s math library rounds a floating-point number up to the nearest integer.
🌐
Medium
elizarov.medium.com › floating-point-for-decimals-fc2861898455
Floating-point for decimals. There is a common misconception that… | by Roman Elizarov | Medium
October 23, 2017 - Moreover, because there are at most 1000 number of up to billion, their sum cannot exceed a trillion (10¹²), so with two digits after decimal point it should comfortably fit into the double-precision floating point type if only we can prevent those errors from accumulating. We need to add our numbers so that 1.01 + 1.02 == 2.03, while a regular addition of those two doubles produces the result of 2.0300000000000002. How? We just need to round them to two decimal digits after the decimal point (we are using Kotlin 1.2 math functions here):
🌐
Sling Academy
slingacademy.com › article › how-to-round-numbers-in-kotlin
How to Round Numbers in Kotlin - Sling Academy
November 29, 2024 - Kotlin leverages the Java standard library for rounding operations. The Math class provides two methods for rounding: Math.round() - Rounds a floating-point number to the nearest integer.
Top answer
1 of 2
1

Rounding usually goes to the nearest integer - 166.66 and 166.99 would both round to 170 since they're both above 166.5. That's the correct (and expected!) behaviour. But the kata tells you to round down, so truncating with toInt() should be fine - so both of those end up as 166.

The problem you're running into is a floating-point rounding error. Here's the one you're having a problem with:

assertEquals(60, dutyFree(377, 40, 9048))

which should work fine:

  • 377 * 0.4 = 150.8
  • 9048 / 150.8 = 60

but actually:

println(377.0 * 0.4)
>> 150.8

println(9048.0 / 150.8)
>> 59.99999999999999

There's a better explanation here, but basically because of the way floating-point math works, there are certain numbers you can't represent accurately, and you end up losing precision. It's similar to how 2/3 in decimal gets written as 0.6666667 - that 7 at the end is a rounding error, and so is 60 getting turned into 59.9999999


You could try using BigDecimal for more accuracy, but that's kinda complicated - what you could do instead, is add a margin of error to compensate for the rounding, by adding a very small number to the result:

fun dutyFree(normPrice: Int, discount:Int, hol:Int) : Int {
    // I just pressed 0 a few times, there's no calculation behind this number, it's just small
    val roundingWindow = 0.000000001
    
    val priseWithDiscount: (Int, Int) -> Double = { a: Int, b: Int -> a*b/100.00}
    val result = hol/priseWithDiscount.invoke(normPrice, discount)
    
   return (result + roundingWindow).toInt()
}

Basically, since you're rounding down anyway, this will only affect numbers that are jussssst under the next integer, by nudging them above it so they get rounded down to that instead. If you make that adjustment too big, it'll start to push much lower numbers up to the next integer, so they're rounded up where they shouldn't be

So you have to use a small number, that's enough to round up those tiny errors (the exact number you need is called the machine epsilon but that's a complicated subject, this is just a "good enough" fix that seems fine for this situation). And when you do that, all your test cases pass!

2 of 2
0

What rule is this applying? if 166.99 -> 167 you want to round it, but if the rule you want to follow is that 166.66 -> 166 you want to truncate the double. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.math/truncate.html