Money type

Steven Schveighoffer schveiguy at gmail.com
Fri Jan 3 14:49:34 UTC 2020


On 1/1/20 4:06 PM, Steven Schveighoffer wrote:
> On 1/1/20 3:20 PM, Ola Fosheim Grøstad wrote:
>> 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.
>>
> 
> It is stored that way. Stored as a long. Just nicer to deal with 
> printing and such. And instead of having to remember the factor, it's 
> stored with the type.

oops, totally misread the floating point part, I thought you said 64 bit 
integer.

It's not a *terrible* idea, but as you accumulate more errors, they will 
add up. In money transactions, most of the time you have a fixed 
resolution, and everything is rounded at every transaction. So floating 
point is not necessary. I'd much rather do integer. I like the 
exactness, and with something like checkedInt, you shouldn't have 
overflow problems.

-Steve


More information about the Digitalmars-d mailing list