Android Question Doubt on Number Format?

Pravee7094

Active Member
Hi all,
Here sample code :
B4X:
Dim fTotal As Double = 100000                                           
Log("fTotal : " & fTotal)
            
Dim Total_Amount As String
Total_Amount = NumberFormat2(fTotal,0,2,2,True)
Log("Final Total Amount : " & Total_Amount)

Result :

fTotal : 100000
Final Total Amount : 100,000.00

Now, Final total amount is 1 lakh.
I want this format : 1,00,000.00

Any Suggestion? or Help ?

Regards
Praveen
 

josejad

Expert
Licensed User
Longtime User
In case you're having problems with your regional settings, you could see this post:

 
Upvote 0

Pravee7094

Active Member
In case you're having problems with your regional settings, you could see this post:

Thanks for your kind response.

I'm from India. So I follow your given code with little change of language and region.
But it's not working for me.

B4X:
Sub DisplayDouble(language As String, region As String, number As Double) As String
    Dim jo As JavaObject
    jo = jo.InitializeNewInstance("java.util.Locale.Builder", Null)
    Dim locale As JavaObject = jo.RunMethodJO("setLanguage", Array As Object(language)).RunMethodJO("setRegion", Array As Object(region)).RunMethod("build", Null)
    Dim numberFormatter As JavaObject = jo.InitializeStatic("java.text.NumberFormat").RunMethod("getNumberInstance", Array As Object(locale))
    Dim retVal As String = numberFormatter.RunMethod("format", Array As Object(number))
    Return retVal
End Sub

B4X:
Log("Value : " & DisplayDouble("en", "IN", 12345678910.69))

Result :
Value : 12,345,678,910.69

I want this type : 12,34,56,78,910.69

Any Help?

Regards
 
Upvote 0

AscySoft

Active Member
Licensed User
Longtime User
you should start creating your custom string formatter function, starting with this
B4X:
Dim fTotal As Double = 12345678910.69
Log("fTotal : " & fTotal)
Dim Total_Amount As String = $"${NumberFormat2(fTotal/100000,1,2,2,True).Replace(".",",")}"$
Log($"Final Total Amount: ₹ ${Total_Amount}L"$ )
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
??
1625132785846.png
 
Last edited:
Upvote 0

Pravee7094

Active Member
see:
I tried these code sample. Still No success with B4j.
Do you got correct format with B4j ? (format : 1,00,000.00)

Sample Code :
B4X:
Sub DisplayDouble2(language As String, region As String, number As Double, minDecimals As Int, maxDecimals As Int) As String
    Dim jo As JavaObject
    jo = jo.InitializeNewInstance("java.util.Locale.Builder", Null)
    Dim locale As JavaObject = jo.RunMethodJO("setLanguage", Array As Object(language)).RunMethodJO("setRegion", Array As Object(region)).RunMethod("build", Null)
    Dim numberFormatter As JavaObject = jo.InitializeStatic("java.text.NumberFormat").RunMethod("getNumberInstance", Array As Object(locale))
    numberFormatter.RunMethod("setMinimumFractionDigits", Array As Object (minDecimals))
    numberFormatter.RunMethod("setMaximumFractionDigits", Array As Object (maxDecimals))
    Dim retVal As String = numberFormatter.RunMethod("format", Array As Object(number))
    Return retVal
End Sub

B4X:
Log("Value : " & DisplayDouble2("en", "IN", 12345678910.69,2,2))

Result :
Value : 12,345,678,910.69

Target Format : 12,34,56,78,910.69

Regards
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
May not very elegant but I think it will work until you found the bug.
B4X:
Sub FormatLakh(number As Double, decimals As Int) As String
    Dim ss As String
    Dim mm As Long = Abs(number)
    Dim nn As Long = mm / 1000
    Dim dc As Double = Abs(number) - mm   
    Dim dd As Int
    Do Until (nn < 100)
        dd = nn Mod 100
        nn = nn / 100
        If ss <> "" Then ss = "," & ss
        ss = dd & ss
    Loop
    Dim lakh As String = $"${NumberFormat2((mm Mod 1000) + dc, 1, decimals, decimals, False)}"$
    If dd > 0 Then
        lakh = ss & "," & lakh
    End If
    If mm > 1000 Then
        lakh = nn & "," & lakh
    End If
    If number < 0 Then lakh = "-" & lakh
    Return lakh
End Sub
 
Upvote 0

Pravee7094

Active Member
May not very elegant but I think it will work until you found the bug.
B4X:
Sub FormatLakh(number As Double, decimals As Int) As String
    Dim ss As String
    Dim mm As Long = Abs(number)
    Dim nn As Long = mm / 1000
    Dim dc As Double = Abs(number) - mm  
    Dim dd As Int
    Do Until (nn < 100)
        dd = nn Mod 100
        nn = nn / 100
        If ss <> "" Then ss = "," & ss
        ss = dd & ss
    Loop
    Dim lakh As String = $"${NumberFormat2((mm Mod 1000) + dc, 1, decimals, decimals, False)}"$
    If dd > 0 Then
        lakh = ss & "," & lakh
    End If
    If mm > 1000 Then
        lakh = nn & "," & lakh
    End If
    If number < 0 Then lakh = "-" & lakh
    Return lakh
End Sub
Yes, It's worked like a charm. Thank you so much.
 
Upvote 0
Top