Money type

IGotD- nise at
Wed Jan 1 22:04:00 UTC 2020

On Wednesday, 1 January 2020 at 20:20:14 UTC, Ola Fosheim Grøstad 
> On Wednesday, 1 January 2020 at 19:01:36 UTC, Steven 
> Schveighoffer wrote:
>> That is what I use. Fixed point with a factor of power of 10. 
>> In other words, a fixed point number with 2 decimal places 
>> would be sufficient for such currency. When doing math on such 
>> types, you just need to deal with the underlying numbers, and 
>> it works fine.
> You don't need fixed point, just store cents in 64 bit floating 
> point and you get at least the same accuracy as a 53 bit 
> integer fixed point.

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.

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.

More information about the Digitalmars-d mailing list