Pat Teruel
06/22/2023, 3:50 AMkevin.cianfarini
06/22/2023, 3:58 AMkevin.cianfarini
06/22/2023, 3:58 AMPat Teruel
06/22/2023, 4:07 AM2.28 * 100 = 227.0
My current latest and quickest solution without using bignum is to instead convert the double to string and manually move the decimal point two places to the right, then parse it into integer.Zac Sweers
06/22/2023, 4:12 AMkevin.cianfarini
06/22/2023, 4:18 AM2.28
data?Pat Teruel
06/22/2023, 4:28 AMPat Teruel
06/22/2023, 6:34 AMx 100
and do arithmetic. Since I encountered this, now I need to really do a big fix for this.kevin.cianfarini
06/22/2023, 6:42 AMCasey Brooks
06/22/2023, 2:47 PM"2.28".toDouble() * 100
, manually split the string on the decimal and convert to dollars and cents only as integers. The problem isn’t that 2.28 * 100
is lossy, it’s that you have the 2.28
value at all.
The only safe way to work with currency is to use the same unit throughout all calculations. Floating-point values are always subject to loss of information (in other words, real-world money literally disappears or is generated), so you need to be using Integer-based calculations through every operation of the entire module. Even without converting to an Integer, FP-values by design can represent much higher numbers than integers, but they do so as the cost of losing precision