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

LucaMs

Expert
Licensed User
Longtime User
I laboriously :mad::) got a routine to get the DecimalFormatSymbols:

B4X:
Public Sub getDecimalSeparator As Char
    Dim rfl As Reflector
    Dim nf As Object = rfl.RunStaticMethod("java.text.NumberFormat", "getInstance", Null, Null)
    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:

LucaMs

Expert
Licensed User
Longtime User
I tried so in a code module:

B4X:
' Process_Globals
Private nativeMe As JavaObject

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

Public Sub getDecimalSeparator(Language As String, Country As String) As Char
    Dim rfl As Reflector
    Dim nf As Object = rfl.RunStaticMethod("java.text.NumberFormat", "getInstance", Null, Null)
    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:
B4X:
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?
 
Upvote 0

picenainformatica

Active Member
Licensed User
Longtime User
Try:

B4X:
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"
 
Upvote 0

LucaMs

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

B4X:
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
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
Maybe you've to change language and country to the values you're searching for when calling
B4X:
rfl.RunPublicmethod("getDecimalFormatSymbols", _
      Array As Object(nativeMe.RunMethod("getNewLocale", Array As Object(Language, Country))), _
      Array As String("java.util.Locale"))

The language codes are two-letter lowercase ISO language codes (such as "en") as defined by ISO 639-1. The country codes are two-letter uppercase ISO country codes (such as "US") as defined by ISO 3166-1.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime 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?
 
Upvote 0

b4auser1

Well-Known Member
Licensed User
Longtime User
More universal:

B4X:
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(1, 2)
 
Last edited:
Upvote 0
Top