I am trying to print multiple doubles with different decimal points, I have search for a while but I cannot find the exact solution for my need.
Let me explain it as an example, so there are a few double variables here:
- 1.23
- 0.330
- 1.00
- 1.001
The result I want:
- 1.23
- 0.33 (unwanted 0 removed)
- 1 (unwanted 0 removed)
- 1.001
Is there any pre-developed syntax for this or do I have to make the algo myself?
My current solution:
fun Double.roundToLastDecimal(): Number {
val num = this.toString().toDouble()
return if (num % 1 == 0.0) {
num.toInt()
} else {
num
}
}