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
 

aeric

Expert
Licensed User
Longtime User
Try again:
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
        If dd < 10 Then
            ss = "0" & dd & ss
        Else
            ss = dd & ss
        End If
    Loop
    Dim lakh As String = $"${NumberFormat2((mm Mod 1000) + dc, 1, decimals, decimals, False)}"$
    If dd > 0 Then lakh = ss & "," & lakh
    If mm > 1000 Then lakh = nn & "," & lakh
    If number < 0 Then lakh = "-" & lakh
    Return lakh
End Sub

B4X:
    Log(FormatLakh(101500.5563, 2))
    Log(FormatLakh(1101500.5563, 2))
    Log(FormatLakh(10101500.5563, 2))
    Log(FormatLakh(110101500.5563, 2))
    Log(FormatLakh(1010101500.5563, 2))
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
If dd < 10 Then
    ss = "0" & dd & ss
Else
    ss = dd & ss
End If
Maybe in new version of B4X, can simplified as:
B4X:
ss = IIf(dd < 10, "0" & dd & ss, dd & ss)

New code will look like this:
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
        ss = IIf(ss <> "", $",${ss}"$, ss)
        ss = IIf(dd < 10, $"0${dd}${ss}"$, $"${dd}${ss}"$)
    Loop
    Dim lakh As String = NumberFormat2((mm Mod 1000) + dc, 1, decimals, decimals, False)
    lakh = IIf(dd > 0, $"${ss},${lakh}"$, lakh)
    lakh = IIf(mm > 1000, $"${nn},${lakh}"$, lakh)
    lakh = IIf(number < 0, $"-${lakh}"$, lakh)
    Return lakh
End Sub
 
Last edited:
Upvote 0

Pravee7094

Active Member
Try again:
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
        If dd < 10 Then
            ss = "0" & dd & ss
        Else
            ss = dd & ss
        End If
    Loop
    Dim lakh As String = $"${NumberFormat2((mm Mod 1000) + dc, 1, decimals, decimals, False)}"$
    If dd > 0 Then lakh = ss & "," & lakh
    If mm > 1000 Then lakh = nn & "," & lakh
    If number < 0 Then lakh = "-" & lakh
    Return lakh
End Sub

B4X:
    Log(FormatLakh(101500.5563, 2))
    Log(FormatLakh(1101500.5563, 2))
    Log(FormatLakh(10101500.5563, 2))
    Log(FormatLakh(110101500.5563, 2))
    Log(FormatLakh(1010101500.5563, 2))
Yes, Its worked. Thanks
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Due to lack of time I attach routine.
If you want you can improve it.

B4X:
    Dim mount As String = NumberFormat2(101500, 0, 2, 2, False)
    Log(GetIndianFormat(mount))
   
    Dim mount As String = NumberFormat2(101500.5563, 0, 2, 2, False)
    Log(GetIndianFormat(mount))

    Dim mount As String = NumberFormat2(1101500.5563, 0, 2, 2, False)
    Log(GetIndianFormat(mount))
   
    Dim mount As String = NumberFormat2(10101500.5563, 0, 2, 2, False)
    Log(GetIndianFormat(mount))

    Dim mount As String = NumberFormat2(110101500.5563, 0, 2, 2, False)
    Log(GetIndianFormat(mount))
   
    Dim mount As String = NumberFormat2(1010101500.5563, 0, 2, 2, False)
    Log(GetIndianFormat(mount))


1625229229047.png

B4X:
Public Sub GetIndianFormat(amount As String) As String
    Dim Value As List
    Value = Regex.Split("\.", amount)
    Dim amountArray() As Char = toCharArray(Value.Get(0))
   
    Dim sb As StringBuilder
    sb.Initialize
    Dim a = 0, b = 0 As Int

    For i = amountArray.Length - 1 To 0 Step -1
        If (a < 3) Then
            sb.Append(amountArray(i))
            a = a + 1
        Else
            If (b < 2) Then
                If (b = 0) Then
                    sb.Append(",")
                    sb.append(amountArray(i))
                    b = b + 1
                Else
                    sb.append(amountArray(i))
                    b = 0
                End If
            End If
        End If
    Next
   
    Dim t As String = sb.ToString
    sb.Initialize
   
    For i = t.Length - 1 To 0 Step -1
        sb.Append(t.CharAt(i))
    Next
    If Value.Size > 1 Then
        Return sb.Append(".").Append(Value.Get(1))
    Else
        Return sb.ToString
    End If
End Sub

Public Sub toCharArray(s As String) As Char()
    Dim out(s.Length) As Char
    For i = 0 To s.Length - 1
        out(i) = s.CharAt(i)
    Next
    Return out
End Sub
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
I found bugs in my function when testing with very big numbers. I just put a test in B4J 9.10 BETA.
Not sure is it something to do with Double precision.

Waiting for debugger to connect...
Program started.
-12,34,56,78,910.56
-2,34,56,78,910.56
-34,56,78,910.56
-4,56,78,910.56
-56,78,910.56
-6,78,910.56
-78,910.56
-8,910.56
-910.56
-10.56
-0.56
0.00
0.56
10.56
910.56
8,910.56
78,910.56
6,78,910.56
56,78,910.56
4,56,78,910.56
34,56,78,910.56
2,34,56,78,910.56
12,34,56,78,910.56
9,12,34,56,78,910.56
89,12,34,56,78,910.55
7,89,12,34,56,78,910.56

-1,01,01,01,01,01,01,01,504.00
-10,01,01,01,01,01,01,500.00
-1,01,01,01,01,01,01,500.50
-10,01,01,01,01,01,500.56
-1,01,01,01,01,01,500.55
-10,01,01,01,01,500.56
-1,01,01,01,01,500.56
-10,01,01,01,500.56
-1,01,01,01,500.56
-10,01,01,500.56
-1,01,01,500.56
-10,01,500.56
-1,01,500.56
1,01,500.56
10,01,500.56
1,01,01,500.56
10,01,01,500.56
1,01,01,01,500.56
10,01,01,01,500.56
1,01,01,01,01,500.56
10,01,01,01,01,500.56
1,01,01,01,01,01,500.55
10,01,01,01,01,01,500.56
1,01,01,01,01,01,01,500.50
10,01,01,01,01,01,01,500.00
1,01,01,01,01,01,01,01,504.00
Program terminated (StartMessageLoop was not called).

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)

    Log(FormatLakh(-12345678910.5553, 2))
    Log(FormatLakh(-2345678910.5553, 2))
    Log(FormatLakh(-345678910.5553, 2))
    Log(FormatLakh(-45678910.5553, 2))
    Log(FormatLakh(-5678910.5553, 2))
    Log(FormatLakh(-678910.5553, 2))
    Log(FormatLakh(-78910.5553, 2))
    Log(FormatLakh(-8910.5553, 2))
    Log(FormatLakh(-910.5553, 2))
    Log(FormatLakh(-10.5553, 2))
    Log(FormatLakh(-0.5553, 2))
    Log(FormatLakh(0, 2))
    Log(FormatLakh(0.5553, 2))
    Log(FormatLakh(10.5553, 2))
    Log(FormatLakh(910.5553, 2))
    Log(FormatLakh(8910.5553, 2))
    Log(FormatLakh(78910.5553, 2))
    Log(FormatLakh(678910.5553, 2))
    Log(FormatLakh(5678910.5553, 2))
    Log(FormatLakh(45678910.5553, 2))
    Log(FormatLakh(345678910.5553, 2))
    Log(FormatLakh(2345678910.5553, 2))
    Log(FormatLakh(12345678910.5553, 2))
    Log(FormatLakh(912345678910.5553, 2))
    Log(FormatLakh(8912345678910.5553, 2))
    Log(FormatLakh(78912345678910.5553, 2))
    Log(" ")
    Log(FormatLakh(-101010101010101500.5553, 2))     ' <-- -1,01,01,01,01,01,01,01,504.00 bug!!!
    Log(FormatLakh(-10010101010101500.5553, 2))      ' <--   -10,01,01,01,01,01,01,500.00 bug!!
    Log(FormatLakh(-1010101010101500.5553, 2))       ' <--    -1,01,01,01,01,01,01,500.50 bug!
    Log(FormatLakh(-100101010101500.5553, 2))        ' <--      -10,01,01,01,01,01,500.56
    Log(FormatLakh(-10101010101500.5553, 2))         ' <--       -1,01,01,01,01,01,500.55 bug?
    Log(FormatLakh(-1001010101500.5553, 2))
    Log(FormatLakh(-101010101500.5553, 2))
    Log(FormatLakh(-10010101500.5553, 2))
    Log(FormatLakh(-1010101500.5553, 2))
    Log(FormatLakh(-100101500.5553, 2))
    Log(FormatLakh(-10101500.5553, 2))
    Log(FormatLakh(-1001500.5553, 2))
    Log(FormatLakh(-101500.5553, 2))
    Log(FormatLakh(101500.5553, 2))
    Log(FormatLakh(1001500.5553, 2))
    Log(FormatLakh(10101500.5553, 2))
    Log(FormatLakh(100101500.5553, 2))
    Log(FormatLakh(1010101500.5553, 2))
    Log(FormatLakh(10010101500.5553, 2))
    Log(FormatLakh(101010101500.5553, 2))
    Log(FormatLakh(1001010101500.5553, 2))
    Log(FormatLakh(10101010101500.5553, 2))        ' <--       1,01,01,01,01,01,500.55 bug?
    Log(FormatLakh(100101010101500.5553, 2))       ' <--      10,01,01,01,01,01,500.56
    Log(FormatLakh(1010101010101500.5553, 2))      ' <--    1,01,01,01,01,01,01,500.50 bug!
    Log(FormatLakh(10010101010101500.5553, 2))     ' <--   10,01,01,01,01,01,01,500.00 bug!!
    Log(FormatLakh(101010101010101500.5553, 2))    ' <-- 1,01,01,01,01,01,01,01,504.00 bug!!!
End Sub

Sub FormatLakh(number As Double, decimals As Int) As String
    Dim dd As Int, ss As String
    Dim mm As Long = Abs(number)
    Dim nn As Long = mm / 1000
    Dim dc As Double = Abs(number) - mm
    Do Until (nn < 100)
        dd = nn Mod 100 : nn = nn / 100
        ss = IIf(ss <> "", $",${ss}"$, ss)
        ss = IIf(dd < 10, $"0${dd}${ss}"$, $"${dd}${ss}"$)
    Loop
    Dim lakh As String = NumberFormat2((mm Mod 1000) + dc, 1, decimals, decimals, False)
    lakh = IIf(dd > 0, $"${ss},${lakh}"$, lakh)
    lakh = IIf(mm > 1000, $"${nn},${lakh}"$, lakh)
    lakh = IIf(number < 0, $"-${lakh}"$, lakh)
    Return lakh
End Sub
 

Attachments

  • FormatLakh.zip
    1.2 KB · Views: 258
Upvote 0

aeric

Expert
Licensed User
Longtime User
I found bugs in my function when testing with very big numbers. I just put a test in B4J 9.10 BETA.
Not sure is it something to do with Double precision.



B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)

    Log(FormatLakh(-12345678910.5553, 2))
    Log(FormatLakh(-2345678910.5553, 2))
    Log(FormatLakh(-345678910.5553, 2))
    Log(FormatLakh(-45678910.5553, 2))
    Log(FormatLakh(-5678910.5553, 2))
    Log(FormatLakh(-678910.5553, 2))
    Log(FormatLakh(-78910.5553, 2))
    Log(FormatLakh(-8910.5553, 2))
    Log(FormatLakh(-910.5553, 2))
    Log(FormatLakh(-10.5553, 2))
    Log(FormatLakh(-0.5553, 2))
    Log(FormatLakh(0, 2))
    Log(FormatLakh(0.5553, 2))
    Log(FormatLakh(10.5553, 2))
    Log(FormatLakh(910.5553, 2))
    Log(FormatLakh(8910.5553, 2))
    Log(FormatLakh(78910.5553, 2))
    Log(FormatLakh(678910.5553, 2))
    Log(FormatLakh(5678910.5553, 2))
    Log(FormatLakh(45678910.5553, 2))
    Log(FormatLakh(345678910.5553, 2))
    Log(FormatLakh(2345678910.5553, 2))
    Log(FormatLakh(12345678910.5553, 2))
    Log(FormatLakh(912345678910.5553, 2))
    Log(FormatLakh(8912345678910.5553, 2))
    Log(FormatLakh(78912345678910.5553, 2))
    Log(" ")
    Log(FormatLakh(-101010101010101500.5553, 2))     ' <-- -1,01,01,01,01,01,01,01,504.00 bug!!!
    Log(FormatLakh(-10010101010101500.5553, 2))      ' <--   -10,01,01,01,01,01,01,500.00 bug!!
    Log(FormatLakh(-1010101010101500.5553, 2))       ' <--    -1,01,01,01,01,01,01,500.50 bug!
    Log(FormatLakh(-100101010101500.5553, 2))        ' <--      -10,01,01,01,01,01,500.56
    Log(FormatLakh(-10101010101500.5553, 2))         ' <--       -1,01,01,01,01,01,500.55 bug?
    Log(FormatLakh(-1001010101500.5553, 2))
    Log(FormatLakh(-101010101500.5553, 2))
    Log(FormatLakh(-10010101500.5553, 2))
    Log(FormatLakh(-1010101500.5553, 2))
    Log(FormatLakh(-100101500.5553, 2))
    Log(FormatLakh(-10101500.5553, 2))
    Log(FormatLakh(-1001500.5553, 2))
    Log(FormatLakh(-101500.5553, 2))
    Log(FormatLakh(101500.5553, 2))
    Log(FormatLakh(1001500.5553, 2))
    Log(FormatLakh(10101500.5553, 2))
    Log(FormatLakh(100101500.5553, 2))
    Log(FormatLakh(1010101500.5553, 2))
    Log(FormatLakh(10010101500.5553, 2))
    Log(FormatLakh(101010101500.5553, 2))
    Log(FormatLakh(1001010101500.5553, 2))
    Log(FormatLakh(10101010101500.5553, 2))        ' <--       1,01,01,01,01,01,500.55 bug?
    Log(FormatLakh(100101010101500.5553, 2))       ' <--      10,01,01,01,01,01,500.56
    Log(FormatLakh(1010101010101500.5553, 2))      ' <--    1,01,01,01,01,01,01,500.50 bug!
    Log(FormatLakh(10010101010101500.5553, 2))     ' <--   10,01,01,01,01,01,01,500.00 bug!!
    Log(FormatLakh(101010101010101500.5553, 2))    ' <-- 1,01,01,01,01,01,01,01,504.00 bug!!!
End Sub

Sub FormatLakh(number As Double, decimals As Int) As String
    Dim dd As Int, ss As String
    Dim mm As Long = Abs(number)
    Dim nn As Long = mm / 1000
    Dim dc As Double = Abs(number) - mm
    Do Until (nn < 100)
        dd = nn Mod 100 : nn = nn / 100
        ss = IIf(ss <> "", $",${ss}"$, ss)
        ss = IIf(dd < 10, $"0${dd}${ss}"$, $"${dd}${ss}"$)
    Loop
    Dim lakh As String = NumberFormat2((mm Mod 1000) + dc, 1, decimals, decimals, False)
    lakh = IIf(dd > 0, $"${ss},${lakh}"$, lakh)
    lakh = IIf(mm > 1000, $"${nn},${lakh}"$, lakh)
    lakh = IIf(number < 0, $"-${lakh}"$, lakh)
    Return lakh
End Sub


It seems it also happen when using NumberFormat2.
B4X:
Log(NumberFormat2(-101010101010101500.5553, 1, 2, 2, True))    ' <-- -101,010,101,010,101,504.00
Log(NumberFormat2(101010101010101500.5553, 1, 2, 2, True))     ' <--  101,010,101,010,101,504.00
 
Upvote 0

Pravee7094

Active Member
On desktop, you can see the Edit link next to Report in your first post. Click on it, you will see the Title edit box appear for you to edit. On mobile, you click the 3 dots next to report in your first post.
I think the Edit link option only appear for Lincensed User. I'm not Licensend User. I could not find any edit link option here:(

edit link.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
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

See:

 
Upvote 0
Top