Android Question What is the best data type for currency value?

amorosik

Expert
Licensed User
To deal with values of type currency and therefore decimals with finite precision, what is the best data type to use in B4A?
I see that there is an additional library BigNumber by Agraham
Is it necessary to use this to get the equivalent of a decimal(18,8) or is there something in the native data types of the B4A environment?
 

emexes

Expert
Licensed User
the equivalent of a decimal(18,8)

That's a large range... 10,000 years of global GDP, down to a millionth of a cent?

For real-world accounting, double-precision floats actually work great, but there are a lot of subtleties about it (some would say traps).

As long as you round-off to cents regularly and religiously, doubles handle a trillion dollars no problem.

A less-controversial method is to account in cents (or hundreths of a cent) using 64-bit integers.

Intriguingly, a couple of years ago I transferred (for my parents) $$$$$$.61 from one bank, and $$$$$$.62 turned up in the receiving bank. If I put the same number through a single-precision float, the same error occurs. I'm guessing the error doesn't happen very often because large amounts are probably done in whole dollars without non-binary decimal fractions.
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Another possibility is to multiply/divide by 100 so to work on integers and avoid the rounding.
Well, maybe "another" ain't the proper term since @emexes already stated "A less-controversial method is to account in cents (or hundreths of a cent) using 64-bit integers." and on a second read it sounds what I suggested above..
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As long as you round-off to cents regularly and religiously, doubles handle a trillion dollars no problem.
I would say that it is better not to round the actual values. Don't use Round or Round2.
Do use NumberFormat or an equivalent method when you want to display the value.
Though to be on the safe side using BigDecimal is the best option.
 
Upvote 0

emexes

Expert
Licensed User
I would say that it is better not to round the actual values. Don't use Round or Round2.
Do use NumberFormat or an equivalent method when you want to display the value.

Need to round whenever spurious fractions of a cent might appear.

eg a $0.44 item with 10% tax, NumberFormat will show as $0.44 plus $0.04 tax (assuming your tax collector is ok with rounding down or to nearest)

but now buy three of them, NumberFormat will show 3 x $0.44 = $1.32 ? and 3 x $0.04 = $0.13 ?

The same issue would occur with BigDecimal.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Makes sense
Makes sense or makes cents? That is the question
Whether 'tis nobler in the mind to suffer
The rounding and fractions of outrageous fortune,
Or to take arms against a sea of troubles


Ok, sorry, I couldn't resist...
 
Upvote 0

Shelby

Well-Known Member
Licensed User
Longtime User
Makes sense or makes cents? That is the question
Whether 'tis nobler in the mind to suffer
The rounding and fractions of outrageous fortune,
Or to take arms against a sea of troubles


Ok, sorry, I couldn't resist...
Shakespurious
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
FYI...

How do banks handle rounding?

It's a commonly used method for rounding taxes. Rather than rounding 0.5 and higher up, and 0.4 and lower down, bankers rounding rounds 0.5 to the nearest even number. Essentially if the cent value of the tax total is odd, the 0.5 (half-cent) rounds upwards; If the cent value is even, the half-cent rounds it downwards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…