Danish Ansari
09/17/2021, 2:07 PMDouble up to certain decimal points, there is round() function in Kotlin but it always completely rounds up to 0 decimals? I want something like round(1.4999,decimalPrecision = 2) // 1.50Joffrey
09/17/2021, 2:11 PM"%.02f".format(1.49999)Danish Ansari
09/17/2021, 2:12 PMJoffrey
09/17/2021, 2:13 PMval decimalPrecision = 2
val doubleFormat = "%.0${decimalPrecision}f"
println(doubleFormat.format(1.4999))
The question is whether you need the rounded value or just a formatted representation of itJoffrey
09/17/2021, 2:17 PMDecimalFormat (if you're on the JVM).Danish Ansari
09/17/2021, 2:21 PMfun Double.round(decimalPrecision: Int): Double {
return "%.0${decimalPrecision}f".format(this).toDouble()
}
but 65312.49999999.round(0) is producing 65312.0 when it should give 63513.0Joffrey
09/17/2021, 2:25 PM63513.0? I guess it should actually be "65312"Danish Ansari
09/17/2021, 2:27 PM65312.5 so rounding 65312.5 one more time should give 65313 , right?Joffrey
09/17/2021, 2:28 PMJoffrey
09/17/2021, 2:28 PMJoffrey
09/17/2021, 2:29 PMBigDecimal insteadDanish Ansari
09/17/2021, 2:32 PMDanish Ansari
09/17/2021, 2:32 PMDanish Ansari
09/17/2021, 2:54 PMfun Double.round(decimalPrecision: Int): Double {
return BigDecimal(this.toString())
.setScale(decimalPrecision, RoundingMode.HALF_UP)
.toDouble()
}
but it's still giving me 1 for 1.4999
Note: I have used that BigDecimal constructor because of this SO answer and I'm infact handling currencyDanish Ansari
09/17/2021, 2:55 PM1.4999 2, or not?
I'm doubting my basic education now 😂Joffrey
09/17/2021, 2:55 PMHALF_ are exactly about dealing with ties (e.g. when it's exactly 1.5), but it's not the case here.
You could of course set the rounding mode to UP but in that case even 1.0001 will be rounded to 2.Joffrey
09/17/2021, 3:00 PMNote: I have used that BigDecimal constructor because of this SO answer and I'm infact handling currency@Danish Ansari It seems you have misunderstood the point of this answer, and my point as well. The problem is not about the constructor of
BigDecimal itself, it's about using double anywhere in the code. If you use doubles, their representation in memory will fail you.
So if you pass a double to this function, you have already lost. You need to start from BigDecimal right away, or from strings. Converting this double value to string before passing to the constructor is unnecessary because it's too late, if any error should happen with double it has already happened.Danish Ansari
09/17/2021, 3:24 PMephemient
09/17/2021, 4:27 PMfun round(x: Double, decimalPrecision: Int): Double {
val scale = 10.0.pow(decimalPrecision)
return round(x * scale) / scale
}CLOVIS
09/20/2021, 8:32 AMJoffrey
09/20/2021, 8:33 AMDanish Ansari
09/20/2021, 8:37 AMCursor#getBigDecimal() in Android SDK only getDouble() 😅
And I am getting data from the local DBCLOVIS
09/20/2021, 8:37 AMCLOVIS
09/20/2021, 8:38 AMCLOVIS
09/20/2021, 8:39 AMDanish Ansari
09/20/2021, 8:45 AMCLOVIS
09/20/2021, 10:07 AMCLOVIS
09/20/2021, 10:08 AMCLOVIS
09/20/2021, 10:10 AMephemient
09/20/2021, 4:39 PM