www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Decimal handling for currency (precision)

reply aberba <karabutaworld gmail.com> writes:
Some suggest working with the lowest currency denomination to 
avoid decimal precision handling and only convert to the highest 
denominations (decimal) when displaying. I've also seen some use 
decimal value handling libraries.

I'm thinking lowest denominations will result in extremely large 
values that D's type system cannot store (if such large values 
makes sense or can happen with money in real life).

What will be your advise on the type to use by default, the 
currency denominations (100p instead of 1.0 dollars), and cost of 
computation.
Nov 23 2017
parent Andrea Fontana <nospam example.com> writes:
On Thursday, 23 November 2017 at 14:47:21 UTC, aberba wrote:
 Some suggest working with the lowest currency denomination to 
 avoid decimal precision handling and only convert to the 
 highest denominations (decimal) when displaying. I've also seen 
 some use decimal value handling libraries.

 I'm thinking lowest denominations will result in extremely 
 large values that D's type system cannot store (if such large 
 values makes sense or can happen with money in real life).

 What will be your advise on the type to use by default, the 
 currency denominations (100p instead of 1.0 dollars), and cost 
 of computation.
From d-money doc: "Here the design decision is to use an integer for the internal representation. This limits the amounts you can use. For example, if you decide to use 4 digits behind the comma, the maximum number is 922,337,203,685,477.5807 or roughly 922 trillion. The US debt is currently in the trillions, so there are certainly cases where this representation is not applicable. However, we can check overflow, so if it happens, you get an exception thrown and notice it right away. The upside of using an integer is performance and a deterministic arithmetic all programmers are familiar with." 922 trillion (+4 digits after comma) should be enough for common computations.
Nov 23 2017