B4J Code Snippet[B4X] Format 10,000 (Ten Thousand) to Lakh

B4J 2.80+
B4X:
``````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 = (mm Mod 1000) + Abs(number) - mm
Dim lakh As String = NumberFormat2(dc, 1, decimals, decimals, False)
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}"\$
Loop
If nn > 0 Then
If (mm Mod 1000) >= 10 And (mm Mod 1000) < 100 Then lakh = \$"0\${lakh}"\$
If (mm Mod 1000) < 10 Then lakh = \$"00\${lakh}"\$
End If
If ss <> "" Then lakh = \$"\${ss},\${lakh}"\$
If nn > 0 Then lakh = \$"\${nn},\${lakh}"\$
If number < 0 Then lakh = \$"-\${lakh}"\$
Return lakh
End Sub``````

B4X:
``````' Using BigNumbers library (B4A, B4J)
' Note: The 5th parameter Unused can be removed. It's a dummy so that the function signature same as NumberFormat2 for quick replace.
Sub LakhFormat2(Number As String, MinIntegers As Int, MaxFractions As Int, MinFractions As Int, Unused As Boolean) As String
Dim bi, si, mm, nn, threedigit, thousand, hundred, ten, zero As BigInteger
Dim bd, sd, od, ad As BigDecimal
thousand.Initialize3(1000)
hundred.Initialize3(100)
ten.Initialize3(10)
zero.Initialize3(0)
bd.Initialize(Number)
od.Initialize5(bd)
nn.Divide(thousand)
threedigit.Initialize7(mm)
threedigit.Remainder(thousand)
Dim id As Int = threedigit.LongValue
bd.SetScale2(MaxFractions, bd.ROUND_HALF_UP)
bi.Initialize6(bd)
sd.Initialize6(bi)
Dim dd As Double = Abs(bd.Subtract(sd).DoubleValue)
Unused = False
Dim lakh As String = NumberFormat2(id + dd, MinIntegers, MaxFractions, MinFractions, Unused)
Dim sb As StringBuilder
sb.Initialize
Do Until (nn.CompareTo(hundred) < 0)
si.Initialize7(nn)
si.Remainder(hundred)
nn.Divide(hundred)
If sb.Length > 0 Then sb.Insert(0, ",")
If si.CompareTo(ten) < 0 Then sb.Insert(0, \$"0\${si.LongValue}"\$) Else sb.Insert(0, \$"\${si.LongValue}"\$)
Loop
If nn.CompareTo(zero) > 0 Then
If id >= 10 And id < 100 Then lakh = \$"0\${lakh}"\$
If id < 10 Then lakh = \$"00\${lakh}"\$
End If
If sb.Length > 0 Then lakh = \$"\${sb.ToString},\${lakh}"\$
If nn.LongValue > 0 Then lakh = \$"\${nn.LongValue},\${lakh}"\$
If od.DoubleValue < 0 Then lakh = \$"-\${lakh}"\$
Return lakh
End Sub``````

Edit (10 Jul 2021): Bug fixed for 1,001.00 and 1,00,000.00
There are known issue with number > 10,000,000,000,000 (1,00,00,00,00,00,000)

Edit (12 Jul 2021): LakhFormat2
Pass number as String to support big numbers
e.g:
B4X:
``````Log(NumberFormat2(-67891245678912345678910.5553, 1, 2, 2, True))    ' Input number converted to Double    -->     -67,891,245,678,912,340,000,000.00
Log(LakhFormat2(-67891245678912345678910.5553, 1, 2, 2, True))      ' Input number converted to Double    -->  -67,89,12,45,67,89,12,34,00,00,000.00
Log(LakhFormat2("-67891245678912345678910.5553", 1, 2, 2, True))    ' Input number as String              -->  -67,89,12,45,67,89,12,34,56,78,910.56``````

Attachments

• FormatLakh.zip
1.3 KB · Views: 159
• LakhFormat2.zip
2.1 KB · Views: 150
Last edited:

aeric

Expert
Longtime User
What' B4J 9.10? Isn't 9.0 the latest?

MikeSW17

Active Member

Thanks. I did first do a search for "B4J 9.1" with no result. Seems I needed to add "Beta" to the search phrase. Got it now

LucaMs

Expert
Longtime User
Is it to format exactly (and only) 10,000 ( as the title "says")?

aeric

Expert
Longtime User
Is it to format exactly (and only) 10,000 ( as the title "says")?
It is not only working for 10,000 but any Double value similar to using NumberFormat2.

Pravee7094

Active Member
B4J 9.10+
B4X:
``````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``````

B4J 9.00
B4X:
``````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
If ss <> "" Then ss = \$",\${ss}"\$
If dd < 10 Then ss = \$"0\${dd}\${ss}"\$ Else ss = \$"\${dd}\${ss}"\$
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``````

There are known issue with number > 10,000,000,000,000 (1,00,00,00,00,00,000)
Still not give correct format for some values.
For example :

B4X:
``Log(FormatLakh(1001, 2))``

Result :
1,1.00

Regards

TILogistic

Expert
Longtime User
Still not give correct format for some values.
For example :

B4X:
``Log(FormatLakh(1001, 2))``

Result :
1,1.00

Regards

Pravee7094

Active Member

View attachment 116121
Yes, Its perfectly worked with your code. Thanks a lot @oparra

aeric

Expert
Longtime User
Still not give correct format for some values.
For example :

B4X:
``Log(FormatLakh(1001, 2))``

Result :
1,1.00

Regards
Should be fixed now. Thanks for reporting.

aeric

Expert
Longtime User
Clean up repeating codes.
B4X:
``````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 = (mm Mod 1000) + 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(dc, 1, decimals, decimals, False)
If nn > 0 Then
If (mm Mod 1000) >= 10 And (mm Mod 1000) < 100 Then lakh = \$"0\${lakh}"\$
If (mm Mod 1000) < 10 Then lakh = \$"00\${lakh}"\$
End If
lakh = IIf(dd > 0, \$"\${ss},\${lakh}"\$, lakh)
lakh = IIf(nn > 0, \$"\${nn},\${lakh}"\$, lakh)
lakh = IIf(number < 0, \$"-\${lakh}"\$, lakh)
Return lakh
End Sub``````

Last edited:

aeric

Expert
Longtime User
I found another issue with 100,000. I am trying to fix now.

aeric

Expert
Longtime User
I found another issue with 100,000. I am trying to fix now.
Code updated in post #1.
Hopefully, no more bugs.

Longtime User
see:

aeric

Expert
Longtime User
A new version using BigNumbers library

B4X:
``````' Using BigNumbers library (B4A, B4J)
' Note: The 5th parameter Unused can be removed. It's a dummy so that the function signature same as NumberFormat2 for quick replace.
Sub LakhFormat2(Number As String, MinIntegers As Int, MaxFractions As Int, MinFractions As Int, Unused As Boolean) As String
Dim bi, si, mm, nn, threedigit, thousand, hundred, ten, zero As BigInteger
Dim bd, sd, od, ad As BigDecimal
thousand.Initialize3(1000)
hundred.Initialize3(100)
ten.Initialize3(10)
zero.Initialize3(0)
bd.Initialize(Number)
od.Initialize5(bd)
nn.Divide(thousand)
threedigit.Initialize7(mm)
threedigit.Remainder(thousand)
Dim id As Int = threedigit.LongValue
bd.SetScale2(MaxFractions, bd.ROUND_HALF_UP)
bi.Initialize6(bd)
sd.Initialize6(bi)
Dim dd As Double = Abs(bd.Subtract(sd).DoubleValue)
Unused = False
Dim lakh As String = NumberFormat2(id + dd, MinIntegers, MaxFractions, MinFractions, Unused)
Dim sb As StringBuilder
sb.Initialize
Do Until (nn.CompareTo(hundred) < 0)
si.Initialize7(nn)
si.Remainder(hundred)
nn.Divide(hundred)
If sb.Length > 0 Then sb.Insert(0, ",")
If si.CompareTo(ten) < 0 Then sb.Insert(0, \$"0\${si.LongValue}"\$) Else sb.Insert(0, \$"\${si.LongValue}"\$)
Loop
If nn.CompareTo(zero) > 0 Then
If id >= 10 And id < 100 Then lakh = \$"0\${lakh}"\$
If id < 10 Then lakh = \$"00\${lakh}"\$
End If
If sb.Length > 0 Then lakh = \$"\${sb.ToString},\${lakh}"\$
If nn.LongValue > 0 Then lakh = \$"\${nn.LongValue},\${lakh}"\$
If od.DoubleValue < 0 Then lakh = \$"-\${lakh}"\$
Return lakh
End Sub``````