Hello,
Is it possible to insert the "." as number format while typing in an Input text?
Thanks, Diego
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.Have you looked at NumberFormat
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
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 tryExample:
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.
Yeah, totally. In my case the app will be used only in my country. We use "."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:
Thankyou Erel, really helpful. But, igot problem when typing char ".". Because, it count char "."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.
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.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
You misunderstood what this code does. The keyboard input type should be number or decimal. This code adds thousands separators automatically.It does not work.
Tried - italian - comma as decimal separator: you cannot insert decimals.Write 1234567 and you will see what is supposed to do.
English - USA - decimals = Dot = OKTried - italian - comma as decimal separator: you cannot insert decimals.
View attachment 89429
because "," is replaced by "".
ok, finally I understand: it works well and does not take into account the different signs of separation.You misunderstood what this code does
Sub GetGroupingSeparator As String
Dim nm As JavaObject
Return nm.InitializeStatic("java.text.DecimalFormatSymbols").RunMethodJO("getInstance", Null).RunMethod("getGroupingSeparator", Null)
End Sub
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
Sub GetDecimalSeparator As String
Dim nm As JavaObject
Return nm.InitializeStatic("java.text.DecimalFormatSymbols").RunMethodJO("getInstance", Null).RunMethod("getDecimalSeparator", Null)
End Sub
Yes, it works.I suppose (not tested) that this code should work to get the decimal separator symbol, then:
I will try both (and maybe... I'll try to create... something).B4X:Sub GetDecimalSeparator As String Dim nm As JavaObject Return nm.InitializeStatic("java.text.DecimalFormatSymbols").RunMethodJO("getInstance", Null).RunMethod("getDecimalSeparator", Null) End Sub