Android Question Specify thousands separator?

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

I have been searching for a way for the user to specify/select their preferred separator in decimal numbers. IE Thousands separator can be comma-dot-space or none.

This has been asked in the distant past but I see no real solution.

Regards Roger
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Depends on AHLocale, Reflection and JavaObject libraries:
B4X:
Sub Process_Globals
   Private nf As AHNumeric
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     nf = SetFormatChars("~", ":")
   End If
   Log(nf.Format(1000000.3434))
End Sub

Sub SetFormatChars(Grouping As Char, DecimalSeparator As Char) As AHNumeric
   Dim loc As AHLocale
   loc.InitializeUS
   Dim jloc As JavaObject = loc
   jloc = jloc.GetField("myLocale")
   Dim nfs As AHNumeric
   nfs.InitializeNumber2(loc)
   Dim r As Reflector
   r.Target = nfs
   Dim dfs As JavaObject
   dfs.InitializeNewInstance("java.text.DecimalFormatSymbols", Array(jloc))
   dfs.RunMethod("setGroupingSeparator", Array(Grouping))
   dfs.RunMethod("setDecimalSeparator", Array(DecimalSeparator))
   Dim jo As JavaObject = r.GetField("mNumberFormat")
   jo.RunMethod("setDecimalFormatSymbols", Array(dfs))
   Return nfs
End Sub
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Depends on AHLocale, Reflection and JavaObject libraries:
B4X:
Sub Process_Globals
   Private nf As AHNumeric
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     nf = SetFormatChars("~", ":")
   End If
   Log(nf.Format(1000000.3434))
End Sub

Sub SetFormatChars(Grouping As Char, DecimalSeparator As Char) As AHNumeric
   Dim loc As AHLocale
   loc.InitializeUS
   Dim jloc As JavaObject = loc
   jloc = jloc.GetField("myLocale")
   Dim nfs As AHNumeric
   nfs.InitializeNumber2(loc)
   Dim r As Reflector
   r.Target = nfs
   Dim dfs As JavaObject
   dfs.InitializeNewInstance("java.text.DecimalFormatSymbols", Array(jloc))
   dfs.RunMethod("setGroupingSeparator", Array(Grouping))
   dfs.RunMethod("setDecimalSeparator", Array(DecimalSeparator))
   Dim jo As JavaObject = r.GetField("mNumberFormat")
   jo.RunMethod("setDecimalFormatSymbols", Array(dfs))
   Return nfs
End Sub


Thanks Erel,

It looks like what I need. Perhaps we could add Numberformat3 to the wish list. Several dedicated options and auto-locale.

Regards Roger
 
Upvote 0
Top