Android Question NumberFormat does not format the zero value

mfstuart

Active Member
Licensed User
Longtime User
Hi all,
I'm formatting numbers from a SQLite table where the stored data type is REAL.
Some of the records (ItemPrice field) have a 0 (zero) value in them. I'd like to use the formatting as such:

B4X:
Dim Price As Double = 0
Price = NumberFormat(cr.GetDouble("ItemPrice"),0,2)

For those records with a value greater than zero, it has 2 decimal places, but when the value is 0 (zero), there is no formatting. It displays as 0. I'd expect 0.00.

Anyone know why this is and how to correctly format zeros?

Thanx,
Mark S.
 
Last edited:

fixit30

Active Member
Licensed User
Longtime User
NumberFormat returns a String, therefore your code should be:

B4X:
Dim Price As String
Price = NumberFormat(cr.GetDouble("ItemPrice"),0,2)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
NumberFormat returns a String!
So you should set:
Dim Price As String
In your case the String returned by the NumberFormat function is converted into a Double!
You may also consider NumberFormat2 where you can define MinimumFractions and MaximumFractions.
 
Upvote 0

mfstuart

Active Member
Licensed User
Longtime User
This worked in formatting the 0 value to display 0.00: (thanx guys)
B4X:
Dim Price as String = ""
Price = NumberFormat2(cr.GetDouble("ItemPrice"),1,2,2,False)

The above I'm now using.

This did not format the 0 value to a display of 0.00:
B4X:
Dim Price as String = ""
Price = NumberFormat(cr.GetDouble("ItemPrice"),1,2)

The above function arguments are misleading as I'd expect the display format to be a minimum of 1 placing in the integer position (in this case would be a 0) and a minimum of 2 placings in the decimal positions - 00

Is this a bug? Seems to me it may be, or very misleading/confusing.
If it worked, I wouldn't be submitting to the forum for answers. I think it needs to be looked at by the products developers.

Thanx,
Mark S.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top