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

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

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
Thanks bro, I will try
 
Upvote 0

Dianzoa

Active Member
Licensed User
Yeah, totally. In my case the app will be used only in my country. We use "."
 
Upvote 0

sonistp

Member
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
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

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

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
Yes, it works.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…