B4J Code Snippet Locale aware Double to String conversion with minimum and maximum decimal control

As per request by @moore_it, this code snippet is a slight modification of a locale aware Double to String method I posted here: https://www.b4x.com/android/forum/threads/solved-setting-a-decimal-separator.105981/#post-663491. The only change I made is that this version allows control over the minimum and maximum number of decimal places to display.

B4X:
Sub DisplayDouble2(language As String, region As String, number As Double, minDecimals As Int, maxDecimals As Int) 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))
   numberFormatter.RunMethod("setMinimumFractionDigits", Array As Object (minDecimals))
   numberFormatter.RunMethod("setMaximumFractionDigits", Array As Object (maxDecimals))
   Dim retVal As String = numberFormatter.RunMethod("format", Array As Object(number))
   Return retVal
End Sub
 

moster67

Expert
Licensed User
Longtime User
Sorry for opening up this thread again but it seems the most appropriate place since @OliverA will probably get a notification but perhaps someone else can help.

I am using Olivers's great code snippet posted above (as is):
B4X:
Sub DisplayDouble2(language As String, region As String, number As Double, minDecimals As Int, maxDecimals As Int) 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))
   numberFormatter.RunMethod("setMinimumFractionDigits", Array As Object (minDecimals))
   numberFormatter.RunMethod("setMaximumFractionDigits", Array As Object (maxDecimals))
   Dim retVal As String = numberFormatter.RunMethod("format", Array As Object(number))
   Return retVal
End Sub

and call it to get a string value in return as follows:
B4X:
Dim impEUR As Double
    Dim dblImp, dblCambio As Double
    Dim strImpEUR As String
    impEUR = dblImp/dblCambio
    strImpEUR = DisplayDouble("it","IT", impEUR)

It works just fine when running the app from the B4J IDE (also in debug).
If I pass on 2324.12 which is retrieved from a SQL database, the string variable returned (strImpEUR) is the Italian equivalent i.e. 2.324,12 which is correct since Italian uses the comma as a decimal separator.

However, if I build a Standalone Package and run the same app, the value returned is 2,324.12.
As you can see, the decimal separator is not changed and remains as a dot.

The computer is the same and regional settings are configured to use Italian. JDK is 11.0.1

I am a bit lost as to why the code snippet is behaving differently. Maybe it is due to something else?

Any ideas?
 
Last edited:
Top