Android Code Snippet [B4X] Lakh format number

Attached is a demo and code to convert numbers to Lakh format.

1625988569657.png
1625989362894.png


B4X:
'Converts the specified number to a string in "Lakh" format.
'The string will include at least Minimum Integers and at most Maximum Fractions digits.
'Example:
'Log(NumberFormatLakh(123456.6789, 0, 2)) '"1,23,456.68"
'Log(NumberFormatLakh(123456, 7 ,0)) '"01,23,456"
Public Sub NumberFormatLakh(Number As Double, MinimumIntegers As Int, MaximumFractions As Int) As String
    Dim value() As String = Regex.Split("\.",NumberFormat(Number, MinimumIntegers, MaximumFractions).Replace(",",""))
    Dim sb As StringBuilder
    sb.Initialize
    Dim sw As Boolean
    For i = value(0).Length - 1 To 0 Step - 1
        If (sb.ToString.Length < 3) Then
            sb.Append(value(0).CharAt(i))
            Continue
        End If
        If Not(sw Or value(0).CharAt(i) = "-") Then
            sb.Append(",").append(value(0).CharAt(i))
            sw = True
        Else
            sb.append(value(0).CharAt(i))
            sw = False
        End If
    Next
    Dim result As StringBuilder
    result.Initialize
    For i = sb.Length - 1 To 0 Step - 1
        result.Append(sb.ToString.CharAt(i))
    Next
    If value.Length > 1 Then
        result.Append(".").Append(value(1))
    End If
    Return result
End Sub

TEST:
 

Attachments

  • NumberFormatLakh.zip
    3.7 KB · Views: 139
Last edited:
Top