Android Question How to print the trailing ".00" of float/double values?

toby

Well-Known Member
Licensed User
Longtime User
When sometime a float value just contains just whole number, for example, 123.00, what's the correct way of printing it out as "123.00" instead of "123"?

B4X:
Dim value As Float=123.00 
    LogColor($"$1.2{value}"$, Colors.Blue)  'printed: 123, 'No trailing ".00" is logged.     expected: 123.00   
    
    value=123.456  'this value works
    LogColor($"$1.2{value}"$, Colors.Blue) 'printed 123.46  expected 123.46


TIA
 

Mahares

Expert
Licensed User
Longtime User
123.00, what's the correct way of printing it out as "123.00"
Use the B4XFormatter library. see below:
B4X:
Private formatter As B4XFormatter  'need B4XFormatter lib

Dim value As Float=123.00
    formatter.Initialize
    Dim DefaultFormat As B4XFormatData = formatter.GetDefaultFormat
    DefaultFormat.MaximumFractions = 2
    DefaultFormat.MinimumFractions = 2
    Log(formatter.Format(value))  'displays 123.00
    
    Log(formatter.Format(123.1))  'displays 123.10
 
Upvote 0

emexes

Expert
Licensed User
Check numberformat
or NumberFormat2

1626567854917.png
 
Upvote 0

emexes

Expert
Licensed User
When sometime a float value just contains just whole number, for example, 123.00

I just realised that if the float value is known to be a whole number, then you could just print it out and then print the ".00" immediately after.

Something of a Gordian Knot solution, but hey, what could possibly go wrong?!?! ?
 
Upvote 0
Top