B4J Question [solved] Setting a decimal separator

Discussion in 'B4J Questions' started by udg, May 20, 2019.

  1. udg

    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
     
  2. OliverA

    OliverA Expert Licensed User

    This?
    Code:
    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:
    Code:
    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:
    Source:
    https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html
     
  3. udg

    udg Expert Licensed User

    Just one word: Perfect

    Thank you
     
  4. Didier9

    Didier9 Active Member Licensed User

    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!
     
    klaus likes this.
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