Android Question B4XFormatter avoid scientific notation

RB Smissaert

Well-Known Member
Licensed User
Longtime User
In the process of converting from B4A to B4X.
As I understand it it is best to avoid using NumberFormat2.
No I need to show the average of a range of integer numbers and don't want to show scientific notation eg: Avg: 1.60018275E7.
I can do this fine with NumberFormat2, but not sure how to do it with B4XFormatter.

This is code to get the average value of a group and count map:

B4X:
Sub GetAvgOfGroupAndCountMap(oMap As Map, strDataType As String) As Double 'ignore
    
    Dim lItems As Long
    Dim Formatter As B4XFormatter 'use this rather than NumberFormat2 to make it B4X compatible
    
    Formatter.Initialize
    
    Dim DefaultFormat As B4XFormatData = Formatter.GetDefaultFormat
    
    DefaultFormat.MaximumFractions = Enums.iAvgRoundingGroupAndCount
    DefaultFormat.MinimumFractions = 0
    DefaultFormat.MinimumIntegers = 0
    DefaultFormat.DecimalPoint = "."
    DefaultFormat.GroupingCharacter = ""
    
    Select Case strDataType
        Case "I", "L"
            Dim lSum As Long
            For Each oKey As Object In oMap.Keys
                lSum = lSum + CLng(oKey) * CLng(oMap.Get(oKey))
                lItems = lItems + CLng(oMap.Get(oKey))
            Next
            Log("lSum: " & lSum)
            Log("lItems: " & lItems)
            Log("NumberFormat2(" & lSum & "/" &  lItems & ", 0, 1, 0, False): " & NumberFormat2(lSum/ lItems, 0, 1, 0, False))
            Return Formatter.Format(lSum / lItems) 'show scientific format
        Case "R"
            Dim dSum As Double
            For Each oKey As Object In oMap.Keys
                dSum = dSum + CDbl(oKey) * CLng(oMap.Get(oKey))
                lItems = lItems + CLng(oMap.Get(oKey))
            Next
            Return Formatter.Format(dSum / lItems)
    End Select

End Sub

Any suggestions how to avoid the scientific notation in B4X?

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
OK, thanks for clearing that up.

RBS
Considered this solved a bit too soon.

When I do:

B4X:
Log(NumberFormat2(lSum / lItems, 0, Enums.iAvgRoundingGroupAndCount, 0, False))

It shows the non-scientific format fine, eg 16.1.
However if that value is passed as a double to the calling Sub it will show in scientific format.
Not sure now how to get rid of that scientific number format.
I think I will just make a simple Sub that does this for me, passing the Double and max decimals.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
B4X:
Dim lSum As Double = 11499228087
Dim lItems As Double = 175
Log(NumberFormat2(lSum / lItems, 0, 1, 0, False))

I get: 65709874.8
OK, my fault.
Although GetAvgOfGroupAndCountMap produced a string it was taken in the calling Sub as a double.
Keeping it all as string makes it work OK.
All solved I think.

RBS
 
Upvote 0
Top