# Currency representation in Lua

18 messages
Open this post in threaded view
|

## Currency representation in Lua

 Hello!Has someone any experience to share about implementing a system that deals with currency values? I have already been bitten by the problems described in http://floating-point-gui.de/ and I want to find a better way.It is mentioned in that guide and elsewhere about using fixed-point arithmetic (https://en.wikipedia.org/wiki/Fixed-point_arithmetic even mentions GnuCash took the same route), and from what I read, it is implemented using scaled integers, pretty much. Is that it? Has anyone some insights? Thank you!PS.: Some side tips in this list do stand out for me, like the http://floating-point-gui.de/ previously mentioned. Thanks!
Open this post in threaded view
|

## Re: Currency representation in Lua

 On Sun, Sep 4, 2016 at 8:20 PM, Diogo Mildenberger wrote: Has someone any experience to share about implementing a system that deals with currency values? I have already been bitten by the problems described in http://floating-point-gui.de/ and I want to find a better way.The two common approaches are fixed point, as you mentioned, and General Decimal Arithmetic, which is basically base-10 floating point. See: http://speleotrove.com/decimal/Ten years ago I made a Lua binding for IBM's General Decimal Arithmetic library decNumber called ldecNumberIt does what you need, but is stuck at Lua 5.1. It depends on some Lua 5.1 features that were removed in 5.2, and would be a bit of work to redo. There have also been some updates to decNumber that ought to be incorporated if ldecNumber is updated. If you work on the library you might consider using decQuad (decimal128) if you can get by with 34 digits of precision; the present version of ldecNumber uses arbitrary precision decNumber set to 69 digits of precision.e
Open this post in threaded view
|

## Re: Currency representation in Lua

 In reply to this post by Diogo Mildenberger > Has someone any experience to share about implementing a system that deals > with currency values? Express your amount in cents, not dollars (or euros or reais). You can then use integers instead of floats. It works even in Lua versions before 5.3
Open this post in threaded view
|

## Re: Currency representation in Lua

 It was thus said that the Great Luiz Henrique de Figueiredo once stated: > > Has someone any experience to share about implementing a system that deals > > with currency values? > > Express your amount in cents, not dollars (or euros or reais). > You can then use integers instead of floats. > It works even in Lua versions before 5.3   Just a quick question---is there a smaller unit than the Euro?  Or Reais? While I can't complain about using cents over dollars [1], I think there might be some issue with currency conversion.   -spc [1] As a citizen of the United States
Open this post in threaded view
|

## Re: Currency representation in Lua

 On Sun, Sep 4, 2016 at 8:24 PM, Sean Conner <[hidden email]> wrote: > It was thus said that the Great Luiz Henrique de Figueiredo once stated: >> > Has someone any experience to share about implementing a system that deals >> > with currency values? >> >> Express your amount in cents, not dollars (or euros or reais). >> You can then use integers instead of floats. >> It works even in Lua versions before 5.3 > >   Just a quick question---is there a smaller unit than the Euro?  Or Reais? > While I can't complain about using cents over dollars [1], I think there > might be some issue with currency conversion. > >   -spc > > [1]     As a citizen of the United States > A one-hundredth division of a euro is also called a cent. The real is divided into 100 centavos, which is a straightforward translation. /s/ Adam
Open this post in threaded view
|

## Re: Currency representation in Lua

 In reply to this post by Sean Conner On 9/5/2016 11:24 AM, Sean Conner wrote: > It was thus said that the Great Luiz Henrique de Figueiredo once stated: >>> Has someone any experience to share about implementing a system that deals >>> with currency values? >> >> Express your amount in cents, not dollars (or euros or reais). >> You can then use integers instead of floats. >> It works even in Lua versions before 5.3 > >    Just a quick question---is there a smaller unit than the Euro?  Or Reais? > While I can't complain about using cents over dollars [1], I think there > might be some issue with currency conversion. Some systems do need more precision than cents. Here is an example from Newark/Element14 for a rather expensive SMD chip resistor (USD): 1+ \$0.083 50+ \$0.016 100+ \$0.012 Switching currencies, here is another item in RM (Ringgit Malaysia): 1+ RM0.56 (RM0.5936) 10+ RM0.09 (RM0.0954) 100+ RM0.05 (RM0.053) The number formatting does not look ideal, but it sure looks like the values are exact. There are items on sale for \$0.001, mostly SMD chip resistors, I haven't seen a smaller price. I buy 100, the total price becomes \$0.10. But, uh, the shopping cart popup reports \$0.00 for unit price, someone messed up there. I'm sure their Finance people and IT people are on top of this thing... Whatever their database is using, it appears to be dealing in exact values. But it does illustrate the complexities of a website for global online sales where individual items can be pretty cheap, pricing can be smaller than 1 cent, etc. -- Cheers, Kein-Hong Man (esq.) Kuala Lumpur, Malaysia
Open this post in threaded view
|

## Re: Currency representation in Lua

 In reply to this post by Diogo Mildenberger On Sun, Sep 04, 2016 at 09:20:13PM -0300, Diogo Mildenberger wrote: > Hello! > > Has someone any experience to share about implementing a system that deals > with currency values? I have already been bitten by the problems described > in http://floating-point-gui.de/ and I want to find a better way. > > It is mentioned in that guide and elsewhere about using fixed-point > arithmetic (https://en.wikipedia.org/wiki/Fixed-point_arithmetic even > mentions GnuCash took the same route), and from what I read, it is > implemented using scaled integers, pretty much. Is that it? Has anyone some > insights? Another thing that is important to remember: Not all currencies are decimal, and actually most currencies are /centimal/. There exist currencies where the subdivision is not 100: 10 and 1000 exist.  Then there are currencies with no subdivision at all, such as Yen. When handling money calculations, you basically have to go and read the Wikipedia article for every currency you're ever likely to handle so you can manage all the hideous corner cases. B.
Open this post in threaded view
|

## Re: Currency representation in Lua

 On 05/09/16 09:39, Rob Kendrick wrote: > When handling money calculations, you basically have to go and read the > Wikipedia article for every currency you're ever likely to handle so you > can manage all the hideous corner cases. It gets worse than that. You have to be careful of the order you perform certain operations on the items you're pricing. Things like; do you apply VAT/sales tax on the total of an order, or on each item. These details will be part of the tax codes or the various regions you're dealing with. I think they'd also mention the precision of the calculations needed (e.g. calculate to 1/10th of a cent) Scott
Open this post in threaded view
|

## Re: Currency representation in Lua

 In reply to this post by Diogo Mildenberger Hi! Am 05.09.2016 03:40 schrieb "Diogo Mildenberger" <[hidden email]>: > Has someone any experience to share about implementing a system that deals with currency values? I have already been bitten by the problems described in http://floating-point-gui.de/ and I want to find a better way. Before jumping to conclusions, I'd like to know what problem you actually encountered (besides from "my numbers don't add up"). Regards, Matthias
Open this post in threaded view
|

## Re: Currency representation in Lua

 In reply to this post by Diogo Mildenberger Double precision floating point can represent all integers up to +-2^53 exactly. You should represent your amounts as the smallest unit of currency, such as cents. Also be sure to round results to the nearest integer after all multiplications and divisions. Additions and subtraction of integer values will be exact.Sent from my Samsung device over Bell's LTE network.-------- Original message --------From: Matthias Kluwe <[hidden email]> Date: 09-05-2016 9:34 AM (GMT-05:00) To: Lua mailing list <[hidden email]> Subject: Re: Currency representation in Lua Hi! Am 05.09.2016 03:40 schrieb "Diogo Mildenberger" <[hidden email]>: > Has someone any experience to share about implementing a system that deals with currency values? I have already been bitten by the problems described in http://floating-point-gui.de/ and I want to find a better way. Before jumping to conclusions, I'd like to know what proble you actually encountered (besides from "my numbers don't add up"). Regards, Matthias
Open this post in threaded view
|

## Re: Currency representation in Lua

 On Mon, Sep 5, 2016 at 2:07 PM, Doug <[hidden email]> wrote: > Double precision floating point can represent all integers up to +-2^53 > exactly. You should represent your amounts as the smallest unit of currency, > such as cents. Also be sure to round results to the nearest integer after > all multiplications and divisions. Additions and subtraction of integer > values will be exact. Nitpick: The smallest unit of currency may not actually be a unit of legal tender; as discussed elsewhere in the thread, the smallest unit might be for example tenths of a cent. /s/ Adam
Open this post in threaded view
|

## Re: Currency representation in Lua

 > On Mon, Sep 5, 2016 at 2:07 PM, Doug <[hidden email]> wrote: > > Double precision floating point can represent all integers up to +-2^53 > > exactly. You should represent your amounts as the smallest unit of currency, > > such as cents. Also be sure to round results to the nearest integer after > > all multiplications and divisions. Additions and subtraction of integer > > values will be exact. > > Nitpick: The smallest unit of currency may not actually be a unit of > legal tender; as discussed elsewhere in the thread, the smallest unit > might be for example tenths of a cent. If you use Lua integers and thousandths of cents as your unit, you will be able to represent up to ~90 trillion USD. That seems enough for most uses. -- Roberto
Open this post in threaded view
|

## Re: Currency representation in Lua

 Tangentially related: There is code for unit conversions on the wiki. [1] I tested it with Lua v. 5.2; haven't tested with 5.3. Best regards, Paul [1]. -- [Notice not included in the above original message:  The U.S. National Security Agency neither confirms nor denies that it intercepted this message.]
Open this post in threaded view
|

## Re: Currency representation in Lua

 In reply to this post by Doug Gale Rounding. That’s a tricky topic. Rounding can mean many things, so be sure to study up on it before assuming you know what to do. On Sep 5, 2016, at 5:07 PM, Doug <[hidden email]> wrote:Also be sure to round results to the nearest integer after all multiplications and divisions.
Open this post in threaded view
|

## Re: Currency representation in Lua

 On 05/09/16 08:28 PM, Tim Hunter wrote: > Rounding. That’s a tricky topic. Rounding can mean many things, so be > sure to study up on it before > assuming you know what to do. > > >> On Sep 5, 2016, at 5:07 PM, Doug <[hidden email] >> > wrote: >> >> Also be sure to round results to the nearest integer after all >> multiplications and divisions. > Oh, don't remind me of that rounding thread where we came up with 30 different code snippets just to round a number. (Anyone has a link to it? I don't feel like looking it up .-.) -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: Currency representation in Lua

 In reply to this post by Diogo Mildenberger From: Diogo Mildenberger [hidden email]Has someone any experience to share about implementing a system that deals with currency values? I have already been bitten by the problems described in http://floating-point-gui.de/ and I want to find a better way. If you are writing financial programs for e.g. mark-to-model present value, risk and so forth, it’s quite typical for these do deal with doubles only [across huge swathes of systems in banks and investment funds].If you are writing an accounting or taxation program, you’ll also need to cope with specific rounding schemes imposed externally in addition to the advice on fixed point given  here, and should think like an accountant (double entry et al) so that you don’t introduce other forms of gap,  e.g. not doing new_balance,old_balance=balance/3,2*balance/3RegardsScott