1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

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

Discussion in 'B4J Code Snippets' started by OliverA, Jul 26, 2019.

  1. OliverA

    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.

    Code:
    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
     
    jmon, AnandGupta, inakigarm and 2 others like this.
  2. moore_it

    moore_it Active Member Licensed User

    GREAT TKS !
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice