Android Question convert number into lakhs with decimal?

Status
Not open for further replies.

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub AppStart (Args() As String)
    Log(FormatWithCustomGrouping(123456, 2))
End Sub


'only tested with positive numbers
Private Sub FormatWithCustomGrouping(n As Double, DecimalPoints As Int) As String
    Dim Groupings() As Int = Array As Int(3, 2, 1000)
    Dim res As StringBuilder
    res.Initialize
    Dim i As Int = n
    Dim s As String = i
    Dim f As Double = (n - i) * Power(10, DecimalPoints)
    res.Insert(0, NumberFormat2(f, DecimalPoints, 0, 0, False))
    res.Insert(0, ".")
    Dim index As Int = s.Length - 1
    For Each g As Int In Groupings
        Do While g > 0
            If index < 0 Then Exit
            res.Insert(0, s.CharAt(index))
            g = g - 1
            index = index - 1       
        Loop
        If index >= 0 Then res.Insert(0, ",")
    Next
    Return res.ToString
End Sub
 
Upvote 0

RishiKumar2210

Active Member
B4X:
Sub AppStart (Args() As String)
    Log(FormatWithCustomGrouping(123456, 2))
End Sub


'only tested with positive numbers
Private Sub FormatWithCustomGrouping(n As Double, DecimalPoints As Int) As String
    Dim Groupings() As Int = Array As Int(3, 2, 1000)
    Dim res As StringBuilder
    res.Initialize
    Dim i As Int = n
    Dim s As String = i
    Dim f As Double = (n - i) * Power(10, DecimalPoints)
    res.Insert(0, NumberFormat2(f, DecimalPoints, 0, 0, False))
    res.Insert(0, ".")
    Dim index As Int = s.Length - 1
    For Each g As Int In Groupings
        Do While g > 0
            If index < 0 Then Exit
            res.Insert(0, s.CharAt(index))
            g = g - 1
            index = index - 1      
        Loop
        If index >= 0 Then res.Insert(0, ",")
    Next
    Return res.ToString
End Sub
Thanks, It worked.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
i need to convert 250000 into 2,50,000.00 is this possible?
 
Upvote 0

RishiKumar2210

Active Member
B4X:
Sub AppStart (Args() As String)
    Log(FormatWithCustomGrouping(123456, 2))
End Sub


'only tested with positive numbers
Private Sub FormatWithCustomGrouping(n As Double, DecimalPoints As Int) As String
    Dim Groupings() As Int = Array As Int(3, 2, 1000)
    Dim res As StringBuilder
    res.Initialize
    Dim i As Int = n
    Dim s As String = i
    Dim f As Double = (n - i) * Power(10, DecimalPoints)
    res.Insert(0, NumberFormat2(f, DecimalPoints, 0, 0, False))
    res.Insert(0, ".")
    Dim index As Int = s.Length - 1
    For Each g As Int In Groupings
        Do While g > 0
            If index < 0 Then Exit
            res.Insert(0, s.CharAt(index))
            g = g - 1
            index = index - 1      
        Loop
        If index >= 0 Then res.Insert(0, ",")
    Next
    Return res.ToString
End Sub
I am using this function with little changes
Dim Groupings() As Int = Array As Int(3, 2, 2 ,3)

This is not working larger than 200 crores (Ex.222,29,70,627.57). Why this is not working?
 
Upvote 0
Status
Not open for further replies.
Top