Bug? Numberformat2 decimal places

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
In iOS
the following code returns a string containing "1000", in Android the code returns a string, containing "1000.0"

I think that this is a bug.


B4X:
           Private n As Double = 1000
            private s as string =  NumberFormat2(n,0,2,0,False)
            log(s)
 

emexes

Expert
Licensed User
In iOS the following code returns a string containing "1000", in Android the code returns a string, containing "1000.0"

I think that this is a bug.

B4X:
           Private n As Double = 1000
            private s as string =  NumberFormat2(n,0,2,0,False)
            log(s)
iOS looks correct. Minimum decimal fraction places is 0.

Perhaps Android is splitting the difference, and giving you the average of 0 and 2 decimal fraction places.
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
@emexes Yes, iOS is correct.

@klaus, I am running OpenJdk 11.0.1. Is that the same as you? I wonder if it is some strange precision problem.

I only noticied it because, I put this post up, https://www.b4x.com/android/forum/t...credit-card-and-apple-pay.124141/#post-775333, and it wasn't working on Android as STRIPE was rejecting the "1000.0" as the number.

In the end, I just frigged it by adding:

B4X:
        else If checkfornumber(o) Then
            #if B4a
            Private n As Int = o
            #else if B4I
            Private n As Double = o
            #End If
            s = s & prevprefix & su.EncodeUrl(actprefix & "["&ky & "]","UTF8")&"="& NumberFormat2(n,0,0,0,False)
        Else

in the function convertsubmaptoUrlEncode in StripeClass
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The java version isn't relevant.
This is not a bug as 1000.0 meets the set minimum and maximum values.

Best to use B4XFormatter. It will work exactly the same in all platforms:
B4X:
Dim f As B4XFormatter
f.Initialize
f.GetDefaultFormat.MaximumFractions = 2
f.GetDefaultFormat.MinimumFractions = 0
f.GetDefaultFormat.GroupingCharacter = ""
Log(f.Format(n))
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I didn't realise that Numberformat2 could do that. I assumed that it would give the minimum possible format.

I tend to use Numberformat2 quite a lot so I will write a little function to take the same parameters and call B4XFormatter instead.
 

klaus

Expert
Licensed User
Longtime User
This is not a bug as 1000.0 meets the set minimum and maximum values.
I would expect, that for 1000 with MiniumFraction = 0, the result beeing 1000 and not 1000.0.
And why not 1000.00 which respects also the requirements?

I am running OpenJdk 11.0.1. Is that the same as you?
I use Java 11.0.1 too.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I would expect, that for 1000 with MiniumFraction = 0, the result beeing 1000 and not 1000.0.
And why not 1000.00 which respects also the requirements?
This happens in the native API. I guess that the default string representation of 1000 is 1000.0 and it doesn't modify it if it meets the requirements.
 
Top