Android Question Thousands separator while typing

Dianzoa

Active Member
Licensed User
Hello,
Is it possible to insert the "." as number format while typing in an Input text?

Thanks, Diego
 

Dianzoa

Active Member
Licensed User
Have you looked at NumberFormat
Yes, I used to use that, but never used for dynamic typing. Probably I have to write the code in text_changed event or something, but I asked in case there is already a library that do this automatically.
 

Erel

Administrator
Staff member
Licensed User
Example:
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
    Dim sb As StringBuilder
    sb.Initialize
    Dim Clean As String = New.Replace(",", "")
    For i = 0 To Clean.Length - 1
        If i Mod 3 = 0 And i > 0 Then
            sb.Insert(0, ",")        
        End If
        sb.Insert(0, Clean.CharAt(Clean.Length - 1 - i))
    Next
    If sb.ToString <> New Then
        EditText1.Text = sb.ToString
        EditText1.SelectionStart = EditText1.Text.Length
    End If
End Sub
Modifying the text while the user types it can be annoying. In this case for example the selection is handled in a simple way where it is always set to the end of the text.
 

Peter Simpson

Expert
Licensed User
Example:
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
    Dim sb As StringBuilder
    sb.Initialize
    Dim Clean As String = New.Replace(",", "")
    For i = 0 To Clean.Length - 1
        If i Mod 3 = 0 And i > 0 Then
            sb.Insert(0, ",")        
        End If
        sb.Insert(0, Clean.CharAt(Clean.Length - 1 - i))
    Next
    If sb.ToString <> New Then
        EditText1.Text = sb.ToString
        EditText1.SelectionStart = EditText1.Text.Length
    End If
End Sub
Modifying the text while the user types it can be annoying. In this case for example the selection is handled in a simple way where it is always set to the end of the text.
This is excellent stuff Erel, I'll save this sub in my code base...
 

Sandman

Well-Known Member
Licensed User

Dianzoa

Active Member
Licensed User
Example:
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
    Dim sb As StringBuilder
    sb.Initialize
    Dim Clean As String = New.Replace(",", "")
    For i = 0 To Clean.Length - 1
        If i Mod 3 = 0 And i > 0 Then
            sb.Insert(0, ",")       
        End If
        sb.Insert(0, Clean.CharAt(Clean.Length - 1 - i))
    Next
    If sb.ToString <> New Then
        EditText1.Text = sb.ToString
        EditText1.SelectionStart = EditText1.Text.Length
    End If
End Sub
Modifying the text while the user types it can be annoying. In this case for example the selection is handled in a simple way where it is always set to the end of the text.
Thanks bro, I will try
 

Dianzoa

Active Member
Licensed User
Considering that this thread doesn't specify that we're talking about a single region of the world, I'd humbly like to remind all that not all regions use the same thousand separator. (And some don't at all.) Lots to find on the net about this, but here are two links I found that might show the complexity of this simple question:
Yeah, totally. In my case the app will be used only in my country. We use "."
 

sonistp

Member
Example:
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
    Dim sb As StringBuilder
    sb.Initialize
    Dim Clean As String = New.Replace(",", "")
    For i = 0 To Clean.Length - 1
        If i Mod 3 = 0 And i > 0 Then
            sb.Insert(0, ",")       
        End If
        sb.Insert(0, Clean.CharAt(Clean.Length - 1 - i))
    Next
    If sb.ToString <> New Then
        EditText1.Text = sb.ToString
        EditText1.SelectionStart = EditText1.Text.Length
    End If
End Sub
Modifying the text while the user types it can be annoying. In this case for example the selection is handled in a simple way where it is always set to the end of the text.
Thankyou Erel, really helpful. But, igot problem when typing char ".". Because, it count char "."
 

Erel

Administrator
Staff member
Licensed User
Code that also handles the decimal separator:
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
    Dim sb As StringBuilder
    sb.Initialize
    Dim Clean As String = New.Replace(",", "")
    Dim DecimalSeparatorIndex As Int = Clean.IndexOf(".")
    If DecimalSeparatorIndex > -1 Then
        sb.Append(Clean.SubString(DecimalSeparatorIndex))
        Clean = Clean.SubString2(0, DecimalSeparatorIndex)
    End If
    For i = 0 To Clean.Length - 1
        If i Mod 3 = 0 And i > 0 Then
            sb.Insert(0, ",")        
        End If
        sb.Insert(0, Clean.CharAt(Clean.Length - 1 - i))
    Next
    If sb.ToString <> New Then
        EditText1.Text = sb.ToString
        EditText1.SelectionStart = EditText1.Text.Length
    End If
End Sub
 

LucaMs

Expert
Licensed User
Code that also handles the decimal separator:
B4X:
Sub EditText1_TextChanged (Old As String, New As String)
    Dim sb As StringBuilder
    sb.Initialize
    Dim Clean As String = New.Replace(",", "")
    Dim DecimalSeparatorIndex As Int = Clean.IndexOf(".")
    If DecimalSeparatorIndex > -1 Then
        sb.Append(Clean.SubString(DecimalSeparatorIndex))
        Clean = Clean.SubString2(0, DecimalSeparatorIndex)
    End If
    For i = 0 To Clean.Length - 1
        If i Mod 3 = 0 And i > 0 Then
            sb.Insert(0, ",")      
        End If
        sb.Insert(0, Clean.CharAt(Clean.Length - 1 - i))
    Next
    If sb.ToString <> New Then
        EditText1.Text = sb.ToString
        EditText1.SelectionStart = EditText1.Text.Length
    End If
End Sub
It does not work.

I tried it setting Italian as language (default, of course, decimal separator is comma) and English U.S.A. (decimal is dot);
in both cases it only accepts the thousands separator - always dot and not immedately visible - and never comma (as thousands or decimals separator).

BTW:
1 - first time I tried with B4J and SelectionStart is read only on it;
2 - I think it is better to have two global variables (process globals) in which to set which are the valid signs as a separator of thousands and decimals.
 
Last edited:

LucaMs

Expert
Licensed User
BTW, if you want to get the locale specific grouping separator then use this code:
B4X:
Sub GetGroupingSeparator As String
    Dim nm As JavaObject
    Return nm.InitializeStatic("java.text.DecimalFormatSymbols").RunMethodJO("getInstance", Null).RunMethod("getGroupingSeparator", Null)
End Sub
I suppose (not tested) that this code should work to get the decimal separator symbol, then:
B4X:
Sub GetDecimalSeparator As String
    Dim nm As JavaObject
    Return nm.InitializeStatic("java.text.DecimalFormatSymbols").RunMethodJO("getInstance", Null).RunMethod("getDecimalSeparator", Null)
End Sub
I will try both (and maybe... I'll try to create... something).
 

LucaMs

Expert
Licensed User
I suppose (not tested) that this code should work to get the decimal separator symbol, then:
B4X:
Sub GetDecimalSeparator As String
Dim nm As JavaObject
Return nm.InitializeStatic("java.text.DecimalFormatSymbols").RunMethodJO("getInstance", Null).RunMethod("getDecimalSeparator", Null)
End Sub
I will try both (and maybe... I'll try to create... something).
Yes, it works.
 
Top