zalewski.se
04/02/2020, 12:31 PMlog(243.0, 3.0) returns 4.999999999999999 😮diesieben07
04/02/2020, 12:33 PMdiesieben07
04/02/2020, 12:34 PMBigDecimalzalewski.se
04/02/2020, 12:36 PMdiesieben07
04/02/2020, 12:37 PMdiesieben07
04/02/2020, 12:37 PMzalewski.se
04/02/2020, 12:39 PMMike
04/02/2020, 1:06 PMtoBigDecimal() extension, and it will always 'do the right thing'.
One major gotcha with the Java BigDecimal is using anything other than the String constructor. If you pass a double/float, then it uses that internally for computations, and you STILL get rounding errors.
The Kotlin extension ensures the BigDecimal constructed does the right thing.Michael de Kaste
04/02/2020, 1:10 PMMike
04/02/2020, 1:17 PMtoBigDecimal() in Kotlin always recommendation.
There are some minor exceptions, as BigDecimal has a few common numbers available as constants (ZERO, ONE, TEN), so use those.
The other 'gotcha' with BigDecimal is precision and scale. If you create BigDecimal("10", 2) and `BigDecimal("`0", 3)`, and then compare them using equals, it will be false. That's because the equals compares scale, too, so the first number is 10.00 and the second is 10.000, so they're obviously not the same ;)Jakub Pi
04/02/2020, 1:47 PMMike
04/02/2020, 1:59 PMequals, and assumes it will do the right thing.
For BigDecimal, us programmers tend to think of 10.0 and 10.00 being equal, or don't even think about this, so it's a common error when using BigDecimal.
Substitute == for equals when using Kotlin.Jakub Pi
04/02/2020, 2:03 PM