Money type

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Wed Jan 1 22:26:30 UTC 2020


On Wednesday, 1 January 2020 at 22:04:00 UTC, IGotD- wrote:
> Using floating point is not recommended. For some fractional 
> number it is actually impossible to store the value as a 
> rational binary number. For example 8.90 would be stored as 
> 8.89999999999+ (in reality this is binary values so just think 
> of my example in an equivalent binary value case). This would 
> lead to some rounding errors, especially when chaining several 
> operations.

No, not if you store as cents. You get the exact same values as 
with 53 bits integers with IEEE754.

You have to pay atttentition to rounding mode, even-odd is common.


> You should go for a representation that always calculates the 
> currency exact, down to the cent or whatever it is. Not doing 
> so you might even be breaking the law for some appliances.

Double will do that fine. As I said, same as integer.  If you 
need half-cents, just multiply with 200 instead of 100. Or you 
could use millis (multiply by 1000).




More information about the Digitalmars-d mailing list