use .ceil

math.ceil(x).toInt()
Answer from Tenobaal on Stack Overflow
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › round-to-int.html
roundToInt | Core API – Kotlin Programming Language
Rounds this Float value to the nearest integer and converts the result to Int. Ties are rounded towards positive infinity. ... import kotlin.math.* import kotlin.test.* fun main() { //sampleStart println(3.14159f.roundToInt()) // 3 ...
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round-to-int.html
Kotlinlang
Raises this value to the integer power n. ... Rounds the given value x towards the closest integer with ties rounded towards even integer.
🌐
Codecademy
codecademy.com › docs › kotlin › math methods › round()
Kotlin | Math Methods | round() | Codecademy
October 27, 2023 - The round() function in Kotlin’s math package rounds a given number to the nearest integer.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › round.html
round | Core API – Kotlin Programming Language
Rounds the given value x towards the closest integer with ties rounded towards even integer. ... import kotlin.math.* import kotlin.test.* fun main() { //sampleStart println(round(3.49)) // 3.0 println(round(3.51)) // 4.0 // 3.5 is between 3.0 ...
🌐
Baeldung
baeldung.com › home › kotlin › kotlin numbers › rounding numbers in kotlin
Rounding Numbers in Kotlin | Baeldung on Kotlin
September 7, 2024 - No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with. As we know, rounding makes a number shorter and simpler at the cost of precision. In this tutorial, we’ll look at some ways in which we can round numbers in Kotlin. The BigDecimal class offers an easy way of rounding Double numbers: val rawPositive = 0.34444 val roundedUp = rawPositive.toBigDecimal().setScale(1, RoundingMode.UP).toDouble() assertTrue(roundedUp == 0.4)
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round.html
round
Raises this value to the integer power n. ... Rounds the given value x towards the closest integer with ties rounded towards even integer.
🌐
W3cubDocs
docs.w3cub.com › kotlin › api › latest › jvm › stdlib › kotlin.math › round-to-int
kotlin.math.roundToInt - Kotlin - W3cubDocs
Rounds this Float value to the nearest integer and converts the result to Int. Ties are rounded towards positive infinity. ... © 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors Licensed under the Apache License, Version 2.0.
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › ceil.html
ceil - Kotlin Programming Language
August 10, 2022 - 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
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › ceil.html
ceil | Core API – Kotlin Programming Language
import kotlin.math.* import ... // -4.0 println(round(-3.49)) // -3.0 //sampleEnd } ... Rounds the given value x to an integer towards positive infinity....
🌐
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.
🌐
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 - Note: The toInt() function discards any decimals without rounding. If you need the nearest integer, use roundToInt() from the kotlin.math package. This function intelligently rounds up or down based on the float value.
🌐
Android Developers
developer.android.com › api reference › roundingmode
RoundingMode | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Bbminfo
bbminfo.com › kotlin › library › math › ceil.php
Kotlin ceil() - Kotlin Math Functions - bbminfo
The Math.ceil() function in Kotlin is used to return the next highest integer value by rounding up value if necessary.
🌐
Suneet Agrawal
agrawalsuneet.github.io › blogs › math-round-vs-math-floor-vs-math-ceil-kotlin
Math.round vs Math.floor vs Math.ceil : Kotlin · Suneet Agrawal
April 11, 2022 - Math.floor rounds up to the nearest Integer which can be equal to or below the actual value.
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

🌐
DhiWise
dhiwise.com › post › converting-kotlin-double-to-int-what-you-need-to-know
Kotlin Double to Int: A Comprehensive Guide
December 5, 2024 - If you want to round the double value to the nearest integer, use the kotlin.math.roundToInt() function.
🌐
Medium
medium.com › @agrawalsuneet › math-round-vs-math-floor-vs-math-ceil-kotlin-49bdfce5491f
Math.round vs Math.floor vs Math.ceil : Kotlin | by Suneet Agrawal | Medium
April 13, 2022 - Before reading this blog, keep in mind that -3 is bigger than -4 and -3.5 is bigger than -3.51 · Math.round rounds up to the nearest Integer which can be above or below or even equal to the actual value.