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
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 ...
🌐
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.
Discussions

java - Round Double to 1 decimal place kotlin: from 0.044999 to 0.1 - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... 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. More on stackoverflow.com
🌐 stackoverflow.com
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
How do you round a number to N decimal places - Support - Kotlin Discussions
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 More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
2
August 2, 2018
Strange behavior of kotlin.math.round

Yeah, it can seem poorly named coming from Java. It is using Math.rint. If you want Math.round, you have to use roundToInt or roundToLong (except Kotlin, unlike the JVM lib, will error on NaN).

More on reddit.com
🌐 r/Kotlin
7
14
March 29, 2018
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round.html
round
Try the revamped Kotlin docs design! ... Mathematical functions and constants. The functions include trigonometric, hyperbolic, exponentiation and power, logarithmic, rounding, sign and absolute value.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › round-to-int.html
roundToInt | Core API – Kotlin Programming Language
x.roundToInt() == Int.MIN_VALUE when x < Int.MIN_VALUE · 1.2 · Illegal · Argument · Exception · when this value is NaN · import kotlin.math.* import kotlin.test.* fun main() { //sampleStart println(3.14159.roundToInt()) // 3 println((-10.0).roundToInt()) // -10 // Values greater than Int.MAX_VALUE are rounded to Int.MAX_VALUE println("(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE is ${(Int.MAX_VALUE.toDouble() + 1.0).roundToInt() == Int.MAX_VALUE}") // true println("Double.POSITIVE_INFINITY.roundToInt() == Int.MAX_VALUE is ${Double.POSITIVE_INFINITY.roundToInt() == Int.M
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round-to-int.html
Kotlinlang
Try the revamped Kotlin docs design! ... Mathematical functions and constants. The functions include trigonometric, hyperbolic, exponentiation and power, logarithmic, rounding, sign and absolute value.
Top answer
1 of 15
174

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.

2 of 15
84

I know some of the above solutions work perfectly but I want to add another solution that uses ceil and floor concept, which I think is optimized for all the cases.

If you want the highest value of the 2 digits after decimal use below code.

import java.math.BigDecimal 
import java.math.RoundingMode
import java.text.DecimalFormat

here, 1.45678 = 1.46

fun roundOffDecimal(number: Double): Double? {
    val df = DecimalFormat("#.##")
    df.roundingMode = RoundingMode.CEILING
    return df.format(number).toDouble()
}

If you want the lowest value of the 2 digits after decimal use below code.

here, 1.45678 = 1.45

fun roundOffDecimal(number: Double): Double? {
    val df = DecimalFormat("#.##")
    df.roundingMode = RoundingMode.FLOOR
    return df.format(number).toDouble()
}

Here a list of all available flags: CEILING, DOWN, FLOOR, HALF_DOWN, HALF_EVEN, HALF_UP, UNNECESSARY, UP

The detailed information is given in docs

Find elsewhere
🌐
W3cubDocs
docs.w3cub.com › kotlin › api › latest › jvm › stdlib › kotlin.math › round
kotlin.math.round - Kotlin - W3cubDocs
Rounds the given value x towards the closest integer with ties rounded towards even integer. ... © 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors Licensed under the Apache License, Version 2.0.
🌐
TutorialsPoint
tutorialspoint.com › kotlin-program-to-round-a-number-to-n-decimal-places
Kotlin Program to Round a Number to n Decimal Places
Rounding of decimal values are done using the ceil() or floor() functions. ... Step 2 ? Declare a float value namely myInput. ... Step 4 ? Use format() to alter the number of decimal places required. Store the result. ... In this example, we will Round a Number to n Decimal Places.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
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 …
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.math › round-to-long.html
roundToLong | Core API – Kotlin Programming Language
Ties are rounded towards positive infinity. Special cases: x.roundToLong() == Long.MAX_VALUE when x > Long.MAX_VALUE · x.roundToLong() == Long.MIN_VALUE when x < Long.MIN_VALUE · 1.2 · Illegal · Argument · Exception · when this value is NaN · import kotlin.math.* import kotlin.test.* fun main() { //sampleStart println(3.14159.roundToLong()) // 3 println((-10.0).roundToLong()) // -10 // Values greater than Long.MAX_VALUE are rounded to Long.MAX_VALUE println("(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX_VALUE is ${(Long.MAX_VALUE.toDouble() * 2.0).roundToLong() == Long.MAX
🌐
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 - 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.
🌐
Luasoftware
code.luasoftware.com › tutorials › kotlin › kotlin-round-double-to-2-decimal-point
Kotlin Round Double to 2 Decimal Point - Lua Software Code
val value = 3.14159265358979323// round to 2 decimal: 3.14"%.2f".format(value).toDouble()// orMath.round(value * 100) / 100.0 ... Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free. Do send some 💖 to @d_luaz or share this article. ... A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz ... Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan. ... Related articlesKotlin Properties Serialization (encode data class to map)Kotlin Convert Data Class to MapKotlin Serialization and Json (Android)Andro
🌐
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 · 中文 – 简体
🌐
Shinyu
shinyu.org › en › kotlin › numbers › rounding-numbers
Kotlin: Rounding numbers
March 14, 2024 - How to: In Kotlin, rounding can be done using several functions like `roundToInt()`, `roundToDouble()`, and using `BigDecimal` for more control.
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.math › round-to-long.html
roundToLong - Kotlin Programming Language
August 2, 2018 - Rounds this Float value to the nearest integer and converts the result to Long.