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

OliverA

Expert
Licensed User
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
 
Top