Bug? NumberFormat & NumberFormat2 not formating decimals

ggpanta

Member
Licensed User
Longtime User
Dim fiat As double
fiat = 6368.4397

NumberFormat(fiat, 0, 2)

Expected:
textFiat = 6368.43

Actual:
textFiat = 6368.4397

Edit: changed fiat to textFiat in expected & actual to make it clearer that its not expected to format the original var but the output of numberforma to a text obj
 
Last edited:

ggpanta

Member
Licensed User
Longtime User
@LucaMs unfortunatelly I cant make it work for some reason

@klaus thats what I am rounding in my code a string and for some reason it doesnt work... textfield1= NumberFormat(fiat, 0, 2) should have only 2 decimals but I am not seeing that :(

I will have to look at the code maybe I am doing something wrong that I am not seeing.
 

LucaMs

Expert
Licensed User
Longtime User
I'm sure that your code is:
textfield1.text = ...

fiat is a String variable?

if so, you can try something like:

B4X:
dim dblFiat as Double = fiat
dblFiat = Round2(dblFiat, 2)
textfield1.text = dblFiat
 

ggpanta

Member
Licensed User
Longtime User
I dont want rounding to happen at all though, I need the extra decimals for calculations but I only want to show 2 decimals in the string. This is a cryptocurrency app and decimals are very important, even 0.0000001 of a coin has actual monetary value, so the decimals must stay as they are, what I show to the user in some parts (summary) just doesnt need them, I am cutting the string now so its ok but for some reason numberformat doesnt want to play.
 

LucaMs

Expert
Licensed User
Longtime User
Try to create a small project for test that contains only the variable and the EditText.

If it does not work, post it here.

(I do not know if this affects, but you can also check the InputType of the EditText)
 

ggpanta

Member
Licensed User
Longtime User
This is the exact code in the app:

B4X:
    JSON.Initialize(APIReply)
    Dim mapAPI As Map
    mapAPI = JSON.NextObject
    Dim fiat As Double
    fiat = mapAPI.Get("last")
    lblExchangeValue.Text = NumberFormat(fiat, 0, 2)

lblExchangeValue is cast as label
 

ggpanta

Member
Licensed User
Longtime User
Its obviously something I am doing, I will retrace the whole thing to check where I am messing up, you can close the post if you want :)
 

ttsolution

Member
Licensed User
Longtime User
Same to me, It works most of time but some time it do not works. If I restart the App it then works

Jonh,
 

Rusty

Well-Known Member
Licensed User
Longtime User
Here is a small example of what is being said:
B4X:
Sub Process_Globals
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim fiat As Double
    fiat = 6368.4397
Log("With decimal values not=zero: " & NumberFormat(fiat, 0, 2))
 
    fiat = 6368.0            '4397
Log("With decimal value of zero: " & NumberFormat(fiat, 0, 2))
 
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub
RESULTS:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
With decimal values not=zero: 6,368.44
With decimal value of zero: 6,368
** Activity (main) Resume **
I have exactly the same problem any double with a .0 comes out as an integer without the .0 decimal. i.e. 91.0 = 91 ; 91.1 = 91.1 ...

Any help is apprecated.
 

klaus

Expert
Licensed User
Longtime User
What exactly do you expect ?
NumberFormat(Number As Double, MinimumIntegers As Int, MaximumFractions As Int)
returns 2 fractions max but removes any non significant zeros.
If you want at least one fraction you should use NumberFormat2:
NumberFormat2(Number As Double, MinimumIntegers As Int, MaximumFractions As Int, MinimumFractions As Int, GroupingUsed As Boolean)
 

Rusty

Well-Known Member
Licensed User
Longtime User
Aha! Thanks Klaus!
I was unaware of numberformat2, don't know how I missed this.
I was expecting the zero to remain...my bad :)
Rusty
 
Top