Android Question [Solved +/-] How to get DecimalSeparator

Discussion in 'Android Questions' started by LucaMs, May 22, 2015.

  1. LucaMs

    LucaMs Expert Licensed User

    I laboriously :mad::) got a routine to get the DecimalFormatSymbols:

    Code:
    Public Sub getDecimalSeparator As Char
        
    Dim rfl As Reflector
        
    Dim nf As Object = rfl.RunStaticMethod("java.text.NumberFormat""getInstance"NullNull)
        rfl.Target = nf
        
    Dim sym As Object = rfl.RunMethod("getDecimalFormatSymbols")
        rfl.Target = sym
        
    Dim Separator As Char = rfl.RunMethod("getDecimalSeparator")
        
    Return Separator
    End Sub
    (why I have not named it DecimalFormatoSymbol? :p ).

    This routine, however, returns the symbol of the default Locale.

    How to adapt it to get a given Locale? (public static DecimalFormatSymbols getInstance (Locale locale))


    I bet that there is a "fast member" who will solve this ;)


    Thank you


    [P.S. "(why I have not named it DecimalFormatoSymbol? :p )"
    because I confused DecimalFormatoSymbol with DecimalSeparator :mad:
    I need to get the DecimalSeparator, thank you]
     
    Last edited: May 22, 2015
  2. LucaMs

    LucaMs Expert Licensed User

    I tried so in a code module:

    Code:
    ' Process_Globals
    Private nativeMe As JavaObject

    ' Sub Init
    nativeMe.InitializeStatic(Application.PackageName & ".modSomeUtils")

    Public Sub getDecimalSeparator(Language As String, Country As StringAs Char
        
    Dim rfl As Reflector
        
    Dim nf As Object = rfl.RunStaticMethod("java.text.NumberFormat""getInstance"NullNull)
        rfl.Target = nf
        
    Dim sym As Object = rfl.RunPublicmethod("getDecimalFormatSymbols", _
          
    Array As Object(nativeMe.RunMethod("getNewLocale"Array As Object(Language, Country))), _
          
    Array As String("java.util.Locale"))
        rfl.Target = sym
        
    Dim Separator As Char = rfl.RunMethod("getDecimalSeparator")
        
    Return Separator
    End Sub


    #If JAVA
    import java.util.Locale;

    public Locale getNewLocale(String Language, String Country) {
    return new Locale(language, country);
    }
    #End If

    getting this error msg:
    Code:
    cannot find symbol
    return new Locale(language, country);
                            ^
      symbol:   variable language
      
    location: class modsomeutils


    It is the second time I get "cannot find symbol" error: is it a my mistake or there is a problem with inline Java?
     
  3. picenainformatica

    picenainformatica Active Member Licensed User

    Try:

    Code:
    dim S as string=numberformat2(1,1,1,false)
    return s.substring2(1,1)
    Looking for second caracter in "1.0" or in "1,0"
     
  4. LucaMs

    LucaMs Expert Licensed User

    Thank you but numberformat2 uses the device locale, I would change it at runtime
     
  5. LucaMs

    LucaMs Expert Licensed User

    I solved this way (but I would prefer to understand more about inline Java, Reflector, JavaObject and if inline java works well):

    Code:
    Dim Local As AHLocale
    Local.Initialize3(Language, Country)

    Dim Dummy As String = AHNumeric1.Format(1.1)
    If Dummy.Contains("."Then
         DecSeparator = 
    "."
    End If
    If Dummy.Contains(","Then
        DecSeparator = 
    ","
    End If
     
  6. inakigarm

    inakigarm Well-Known Member Licensed User

    Maybe you've to change language and country to the values you're searching for when calling
    Code:
    rfl.RunPublicmethod("getDecimalFormatSymbols", _
          
    Array As Object(nativeMe.RunMethod("getNewLocale"Array As Object(Language, Country))), _
          
    Array As String("java.util.Locale"))
     
  7. LucaMs

    LucaMs Expert Licensed User

    Thanks @inakigarm, but they are right, I'm sure.
     
  8. RandomCoder

    RandomCoder Well-Known Member Licensed User

    I have a simpler way, why not just iterate through the string starting at position 1 (not zero so as to avoid any sign if present) and return the first non-numeric character?
     
  9. LucaMs

    LucaMs Expert Licensed User

    Thank you for you answer, @RandomCoder.

    The question was how to get "the string". I solved this way #5, thank you.
     
    RandomCoder likes this.
  10. b4auser1

    b4auser1 Well-Known Member Licensed User

    More universal:

    Code:
    Dim Locale As AHLocale
    Locale.Initialize3(Language, Country)

        
    Dim Numeric As AHNumeric : Numeric.Initialize2(Locale)

        
    Dim Dummy As String = Numeric.Format(1.1)
        
    Return Dummy.SubString2(12)
     
    Last edited: May 23, 2015
    picenainformatica likes this.
  11. LucaMs

    LucaMs Expert Licensed User


    Yes, I used .Contains thinking to values like "1.234,56" or "1,234.56" but given I used "1.1"... :D

    I'm tired :)
     
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