www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Decimal/Currency Type

reply Zane <zane.sims gmail.com> writes:
There has been a question or two about this before, but I really 
don't feel there has been a reasonable answer (or I have missed 
it).

I am wondering if there is a fixed decimal type, either built-in 
or implemented in Phobos (similar to python's "Decimal" or Java's 
"BigDecimal").

Previous posts suggested using a long and keeping track of the 
decimal point, but there are a few problems with that. First, 
arithmatic with, or assignments from, other  types wouldn't 
translate properly (the built in types have no awareness of the 
decimal point location associated with the long). Second, if 
using for currency as an example, I could probably get by with 
long if only tracking cents, but what if I wanted to track 
thousandths of a cent or more? That wouldn't leave me with much 
place values to track large dollar amounts.

Is there such a thing build into D? If not, is there any plans to 
add it? It seems like this would be something needed by many 
applications (especially anything dealing with currency).
Aug 09 2016
next sibling parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Tuesday, 9 August 2016 at 10:48:50 UTC, Zane wrote:
 Is there such a thing build into D? If not, is there any plans 
 to add it? It seems like this would be something needed by many 
 applications (especially anything dealing with currency).
I don't think there is anything in the standard library, but there is a dub library: https://github.com/qznc/d-money
Aug 09 2016
parent qznc <qznc web.de> writes:
On Tuesday, 9 August 2016 at 11:14:09 UTC, Edwin van Leeuwen 
wrote:
 On Tuesday, 9 August 2016 at 10:48:50 UTC, Zane wrote:
 Is there such a thing build into D? If not, is there any plans 
 to add it? It seems like this would be something needed by 
 many applications (especially anything dealing with currency).
I don't think there is anything in the standard library, but there is a dub library: https://github.com/qznc/d-money
This is the "get by with long, but" approach. The downside is the limited range. The upside is (probably) speed.
Aug 09 2016
prev sibling next sibling parent reply Andre Pany <andre s-e-a-p.de> writes:
On Tuesday, 9 August 2016 at 10:48:50 UTC, Zane wrote:
 Is there such a thing build into D? If not, is there any plans 
 to add it? It seems like this would be something needed by many 
 applications (especially anything dealing with currency).
Hi, the lack of a decimal type in Phobos is known and it is added to the vision list [1]. In addition to the dub package there is one in the review queue [2]. Also it seems there is a C package from IBM?, which could might be included into phobos via a binding. [1] https://wiki.dlang.org/Vision/2016H2 [2] https://wiki.dlang.org/Review_Queue Kind regards André
Aug 09 2016
parent Zane <zane.sims gmail.com> writes:
Thanks to all who replied (especially Andre). I am glad to hear 
that it is part of the plan to include this eventually (it's also 
nice to read some of the other plans I was not familiar with). I 
don't use D for very serious projects yet, but have been 
following it for years for its long-term potential. I just wanted 
to make sure fixed-point decimal was being considered for the 
standard instead of being swept under the rug because of the 
previous "long" suggestions.

Thanks again!
Aug 09 2016
prev sibling parent Gary Willoughby <dev nomad.so> writes:
On Tuesday, 9 August 2016 at 10:48:50 UTC, Zane wrote:
 There has been a question or two about this before, but I 
 really don't feel there has been a reasonable answer (or I have 
 missed it).

 [...]
There is one in the review queue since forever. https://wiki.dlang.org/Review_Queue Maybe take a look at that?
Aug 10 2016