Android Question Rounding of numbers for currencies

William Hunter

Active Member
Licensed User
Longtime User
When rounding numbers for currencies, is there a preference between Round2 or NumberFormat2? I know that Erel prefers NumberFormat2 for rounding, but I am wondering if there is any special consideration required for rounding currencies.
B4X:
CurrentTask.ToValue.Text = Round2(CurrentTask.FromValue.Text * Rate, 2)
or
CurrentTask.ToValue.Text = NumberFormat2(CurrentTask.FromValue.Text * Rate, 0, 2, 2, False)

Best regards :)
 

DonManfred

Expert
Licensed User
Longtime User
Use NumberFormat if you want to have a specific result.
Store the value as Double or Float in your database.
Use Numberformat to format the output
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The NumberFormat methods are best suited to displaying data as they return strings. Round returns a Long type and Round2 returns a Double type. For calculations that you are going to store in another variable, I would use the one that returns the correct type. Floats are not best suited to storing monetary values as they can very quickly introduce rounding errors.

As B4x happily converts from strings to Numbers (providing the string correctly parses to a number), then NumberFormat2 or Round2 should provide the same results. There may be some performance gains in using Round2 if that is a concern for you.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Another "trick" (if your currency amounts fell in the long range) could be to multiply each amount by 100, thus avoiding decimals and rounding errors at all. Before display you divide the "augmented" amount by 100 and use NumberFormat2 to properly show it..

udg
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Another "trick" (if your currency amounts fell in the long range) could be to multiply each amount by 100, thus avoiding decimals and rounding errors at all. Before display you divide the "augmented" amount by 100 and use NumberFormat2 to properly show it..

udg
Thank you udg. When you refer to amounts in the long range, do you mean large numbers? Or, do you mean numbers that are Longs?

Best regards :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
In the code you posted in the first post there is no difference between the two methods.

NumberFormat is the preferred method. It encourages you to keep the most accurate value (stored in the Double variable) and only round the number when you display it to the user.

Note that you can also use the smart string literal:
B4X:
CurrentTask.ToValue.Task = $"$1.2{CurrentTask.FromValue.Text * Rate}"$
This is the same as using NumberFormat.

If you are doing many calculations then it is better to use BigDecimal to avoid rounding errors:
https://www.b4x.com/android/help/bignumbers.html
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi William,
I suggested the use of a long data type, or even int if that fits well with the kind of amounts your software has to deal with. You may want to consider the arithmetic operations you need to perform on those amounts. For a simple price * quantity and subsequent total the trick works well, so for VATs .
I never tested its performance against the Bigdecimal data type.
 
Upvote 0
Top