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 OverflowDouble.toString() already does most of this for you - it just retains as many digits as necessary to distinguish the number from other doubles, so it'll strip the trailing zeroes.
The only issue is wanting 1.0 represented as 1, since that's technically an integer representation, not a double. You can write a function to take care of that case though:
// or a val with a get() to make the string representation a property
fun Double.roundToString() = when {
toInt().toDouble() == this -> toInt()
else -> this
}.toString()
Just FYI, if the number's small enough (less than 10¯³) toString() will produce scientific notation instead of listing all the digits with leading zeroes. If you want to handle those, you'll probably have to work with format strings, decide how much precision you want, etc.
Here's a way you can do that, with up to 10 digits of precision, and any trailing zeroes removed (and the decimal point itself, if it ends up at the end):
fun Double.roundToString() = "%.10f".format(this).trimEnd('0').trimEnd('.')
or using a regex if you're into that
fun Double.roundToString() = "%.10f".format(this).replace(Regex("\\.*0+$"), "")
I would use a simple extension function for Double, maybe like this:
fun Double.printMinimalRepresentation() {
// check if the decimal places equal zero
if (this % 1 > 0.0) {
// if not, just print the number
println(this)
} else {
// otherwise convert to Int and print
println(this.toInt())
}
}
Example values in a main function:
fun main() {
var a: Double = 1.23
var b: Double = 0.330
var c: Double = 1.00
var d: Double = 1.001
a.printMinimalRepresentation()
b.printMinimalRepresentation()
c.printMinimalRepresentation()
d.printMinimalRepresentation()
}
This prints
1.23
0.33
1
1.001
Please note that the conversion toInt() not only removes the unwanted zero but the obviously unwantet dot as well.
I recently started to learn Kotlin. And for now I'm enjoying it. But I wanted to re-create some old tasks from C# because I wanted to get used to Kotlin. But one of the tasks demanded to leave 2 decimal numbers after the dot.
I wonder how can I use Math.round() in order to achive this. Because I personally don't think |"%.2f".Format(value)| it's a reliable way.The picture is me trying to figure it out. Thanks in advance.