B4J Question [solved] Setting a decimal separator

udg

Expert
Licensed User
Hi all,

I'm aware of a few ways to set a decimal separator so to output a double according to locale settings or whatever is needed, but I'd like to know if it can be easily done by using JavaObject alone.

To be clear, I saw the thread about using AHlocale+JO+Reflection
I know how to do it substituting a dot with a comma (or whatever) on a string from NumberFormat/2
I Know about B4X Formatter.

My goal is to learn something new, not to solve a specific problem.

So, how to set the decimal separator just using JO? TIA
 

OliverA

Expert
Licensed User
This?
B4X:
Sub DisplayDouble(language As String, region As String, number As Double) As String
   Dim jo As JavaObject
   jo = jo.InitializeNewInstance("java.util.Locale.Builder", Null)
   Dim locale As JavaObject = jo.RunMethodJO("setLanguage", Array As Object(language)).RunMethodJO("setRegion", Array As Object(region)).RunMethod("build", Null)
   Dim numberFormatter As JavaObject = jo.InitializeStatic("java.text.NumberFormat").RunMethod("getNumberInstance", Array As Object(locale))
   Dim retVal As String = numberFormatter.RunMethod("format", Array As Object(number))
   Return retVal
End Sub
Usage:
B4X:
Log(DisplayDouble("fr", "FR", 12345678910.69))
Log(DisplayDouble("en", "US", 12345678910.69))
Log(DisplayDouble("en", "UK", 12345678910.69))
Log(DisplayDouble("de", "DE", 12345678910.69))
Output:
12 345 678 910,69
12,345,678,910.69
12,345,678,910.69
12.345.678.910,69
Source:
https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html
 

udg

Expert
Licensed User
Just one word: Perfect

Thank you
 

Didier9

Active Member
Licensed User
B4X:
Log(DisplayDouble("fr", "FR", 12345678910.69))
Output:
12 345 678 910,69
As an aside, it is very interesting and very weird because going to school in France (admittedly quite some time ago) I clearly remember using the decimal point as a separator for blocks of 3 digits.
This page (in French, sorry) http://villemin.gerard.free.fr/Wwwgvmm/Numerati/Bloc3.htm clearly says this was a relatively recent change (2003) due to international normalization.
Learn something every day :)
Thank you for the memory trip!
 
Top