Bug? NumberFormat(Number,0,2): Works on Android but doesn't work in B4J

LWGShane

Well-Known Member
Licensed User
Longtime User
If you use NumberFormat(1000000,0,2) on Android it correctly displays as "1,000,000" but displays as "1000000" on B4J.

B4J:
1582376881013.png


B4A:
1582376874523.png
 
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
It also works for me. Are you sure that you are not using NumberFormat2(1000000,0,0,2,False) on B4J?

Edit: You are right, this is what I get
B4X:
Log(NumberFormat2(1000000,0,0,2, False))    '1000000.00  on B4J Version 8.10

Log(NumberFormat(1000000,0,2))                '1000000.00  on B4J Version 8.10

Log(NumberFormat(1000000,0,2))                '1,000,000  on B4A Version 9.80
 
Last edited:

William Lancee

Well-Known Member
Licensed User
Longtime User
I was confused by inconsistency, but now I agree there is a Bug (hard to believe)! It shows up when you run these 3 lines on B4J.

B4X:
Log(NumberFormat(1000000,0,2))                '1,000,000   on B4J Version 8.10
Log(NumberFormat2(1000000,0,0,2,False))        '1000000.00  on B4J Version 8.10
Log(NumberFormat(1000000,0,2))                '1000000.00  on B4J Version 8.10
 

William Lancee

Well-Known Member
Licensed User
Longtime User
After some experimentation, it appears that when NumberFormat call comes after a NumberFormat2 call, the separator flag is not cleared.
B4X:
Log(NumberFormat2(1000000,0,0,2,False))      '1000000.00  on B4J Version 8.10
Log(NumberFormat(1000000,0,2))                '1000000.00  on B4J Version 8.10

Log(NumberFormat2(1000000,0,0,2,True))      '1,000,000.00  on B4J Version 8.10
Log(NumberFormat(1000000,0,2))                '1,000,000.00  on B4J Version 8.10

And the maximum fractions is also inherited!

B4X:
    Log(NumberFormat2(1000000,0,2,0,False))        '1000000  on B4J Version 8.10
    Log(NumberFormat(1000000,0,2))                '1000000  on B4J Version 8.10

    Log(NumberFormat2(1000000,0,2,0,True))        '1,000,000  on B4J Version 8.10
    Log(NumberFormat(1000000,0,2))                '1,000,000  on B4J Version 8.10
 
Last edited:

Sandman

Expert
Licensed User
Longtime User
I'm away from my B4* products at the moment so I can't do any hands-on testing myself, but I have to ask if this could be explained by different regional settings on the Android device and the OS where the B4J app is run?

I would imagine that NumberFormat takes the regional settings account, isn't that correct? (The spec up top doesn't mention anything about this, but it seems very strange that it would always default to comma being used as a thousands separator.)
 

agraham

Expert
Licensed User
Longtime User
Yes, looking inside jCore.jar this seems to be this sems to be a bug in B4J. In B4A in Core.jar NumberFormat and NumberFormat2 use separate instances of a java.text.NumberFormat object when formatting numbers. In B4J they share a common single instance so NumberFormat inherits the latest Grouping and MinimumFractions settings from NumberFormat2!
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Using only NumberFormat2 avoids the problem.
Also B4XFormatter doesn't have this problem. The following are correct and work the same in B4A and B4J.

B4X:
    'Private formatter As B4XFormatter in internal Library
    formatter.Initialize
    Dim DefaultFormat As B4XFormatData = formatter.GetDefaultFormat
    DefaultFormat.MaximumFractions = 2
    DefaultFormat.MinimumFractions = 2
  
    'In . grouping Locales
    Log(formatter.Format(2131231231.12))

   'In , grouping Locales
    DefaultFormat.DecimalPoint = ","
    DefaultFormat.GroupingCharacter = "."
    Log(formatter.Format(2131231231.123456))
 
Last edited:
Top