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
 

Erel

B4X founder
Staff member
Licensed User
Longtime 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.
 
Upvote 0

Peter Simpson

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

Sandman

Expert
Licensed User
Longtime User
Upvote 0

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
 
Upvote 0

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 "."
 
Upvote 0

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 "."
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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
 
Upvote 0

LucaMs

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

LucaMs

Expert
Licensed User
Longtime User
You misunderstood
This is true very often 😄:(

it.gif


us.gif
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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).
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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.
 
Upvote 0
Top