Android Question [SOLVED] NumberFormat - Parse

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

  1. LucaMs

    LucaMs Expert Licensed User

    If I'm not mistaken, NumberFormat should also provide a Parse method, as DateTime.

    [developer.android.com/reference
    You can also use a NumberFormat to parse numbers:
    myNumber = nf.parse(myString);]

    I tried to use inline Java:

    Code:
    #If JAVA
    public Double NumberFormat3(String value) {
        NumberFormat nf = NumberFormat.getInstance(Locale.getLanguage);
        return nf.parse(value);
    }
    #End If
    but I get:
    java:434: error: cannot find symbol
    NumberFormat nf = NumberFormat.getInstance(Locale.getLanguage);
    ^

    Is this due to the fact that B4A already uses "a" NumberFormat?

    How can I refer to the abstract class NumberFormat using Reflector?

    Code:
    Public Sub NumberFormat3(Value As Double) As String
        
    Dim R As Reflector
        R.Target =?

    Thank you
     
  2. DonManfred

    DonManfred Expert Licensed User

    have you added the import?

    Code:
    import java.text.NumberFormat;
     
  3. LucaMs

    LucaMs Expert Licensed User

    No.

    Thank you, Speedy.

    Now I have to import something else because the error now is on Locale.
     
    Last edited: May 16, 2015
  4. LucaMs

    LucaMs Expert Licensed User

    Uhm...

    this works and returns the correct string based on the currency of the device.
    Code:
    NativeMe.InitializeContext
    ...
    Dim Value As Double = 5123.45
    Dim StringValue As String = NativeMe.RunMethod("NumberFormat3"Array As Object(Value))
    Log(StringValue)
    Log(NumberFormat(Value, 12))
    ...

    #If JAVA
    import java.text.NumberFormat;
    import java.util.Locale;
    public String NumberFormat3(Double Value) {
       return NumberFormat.getInstance().format(Value);
    }
    #End If
    on my device (italian):
    5,123.45
    5.123,45



    Parse unfortunately does not work.
    Code:
    'Dim StringValue As String = "5123,45"
    Dim StringValue As String = "5123,45"
    Dim Value As Double = NativeMe.RunMethod("NumberParse"Array As Object(StringValue))
    Log(Value)

    #If JAVA
    import java.text.NumberFormat;
    import java.util.Locale;
    public Number NumberParse(String value) {
        NumberFormat nf = NumberFormat.getInstance();
        return nf.parse(value);
    }
    #End If

    435: error: unreported exception ParseException; must be caught or declared to be thrown
    return nf.parse(value);
    ^

    (how catch the exception? "LucaMs, please open a new thread for this question" :D)
     
    Last edited: May 16, 2015
  5. LucaMs

    LucaMs Expert Licensed User

    I can't find the error!

    The code should be simply:
    Code:
    #If JAVA
    import java.text.NumberFormat;
    import java.util.Locale;

    public String NumberFormat3(Double Value) {
        return NumberFormat.getInstance().format(Value);
    }
    public Number NumberParse(String Value) {
        return NumberFormat.getInstance().parse(Value);
    }
    #End If
    but at compile time:
    Code:
    java:437: error: unreported exception ParseException; must be caught or declared to be thrown
       
    return NumberFormat.getInstance().parse(Value);
                                              ^
     
    Last edited: May 16, 2015
  6. DonManfred

    DonManfred Expert Licensed User

    B4A does not know a Object Number

    What kind of number do you want? int, float, real, double?

    try something like this

    Code:
    public double NumberParseDouble(final BA ba,String value) {
        
    NumberFormat nf = NumberFormat.getInstance();
        
    try {
                
    return nf.parse(value).doubleValue();
                //
    return nf.parse(value).floatValue():
                //
    return nf.parse(value).intValue();
                //
    return nf.parse(value).longValue();
        
    } catch (ParseException e) {
                // TODO Auto-generated catch block
                ba.Log("ParseException");
            }
        }
     
    Last edited: May 16, 2015
  7. LucaMs

    LucaMs Expert Licensed User


    and what should I pass as "final BA ba"?
     
  8. DonManfred

    DonManfred Expert Licensed User

    ahh, mom... have had a look at the inline java tut...

    try this

    Code:
    public double NumberParseDouble(String value) {
        
    NumberFormat nf = NumberFormat.getInstance();
        
    try {
                
    return nf.parse(value).doubleValue();
                //
    return nf.parse(value).floatValue():
                //
    return nf.parse(value).intValue();
                //
    return nf.parse(value).longValue();
        
    } catch (ParseException e) {
                // TODO Auto-generated catch block
                BA.Log("ParseException");
            }
        }
    BA should always are set after you initialize the nativeme
     
  9. LucaMs

    LucaMs Expert Licensed User

    java:437: error: unreported exception ParseException; must be caught or declared to be thrownreturnNumberFormat.getInstance().parse(Value);
    ----------------------------------------------------------------------------------------------------------------------------------^
    Strangely, the "cursor" in the error log indicates the parenthesis after "parse", as if the method parse does not require a parameter (or as parse does not exists at all).
     
  10. LucaMs

    LucaMs Expert Licensed User


    Then this also should work, but not:
    Code:
    public Double NumberParse(String Value) {
        
    return NumberFormat.getInstance().parse(Value).doubleValue();
    }

    I think that, for some strange reason, "parse" is not a valid method!
     
  11. LucaMs

    LucaMs Expert Licensed User

    Code:
    public Integer NumParse() {
    NumberFormat nf = NumberFormat.getInstance();
    Number myNumber = nf.parse(
    "1.5");
    return 0;
    }
    Same error, without parameter and returning 0 as Integer.
    "parse" is not recognized.
     
  12. LucaMs

    LucaMs Expert Licensed User

    Something does not work in inline Java, I think.

    I found this simple example and I converted it in this way:
    Code:
    public Integer NumParse() {
    NumberFormat nf = NumberFormat.getInstance();
    Number number = nf.parse(
    "1123.12");
    return 0;
    }
    same error, Parse is not recognized.
     
  13. LucaMs

    LucaMs Expert Licensed User

    Well, I found a solution using Reflection instead of inline Java:
    Code:
    Public Sub StrNumParse(StrNum As StringAs Double
        
    Dim rfl As Reflector
        
    Dim nf As Object = rfl.RunStaticMethod("java.text.NumberFormat""getInstance"NullNull)
        rfl.Target = nf
        
    Dim dbl As Double = rfl.RunMethod2("parse", StrNum, "java.lang.String")
        
    Return dbl
    End Sub
     
    RandomCoder and DonManfred like this.
  14. b4auser1

    b4auser1 Well-Known Member Licensed User

    Why not to use auto conversion ?
    Code:
    Dim StrNum As String = "123.78"
    If IsNumber(StrNum) Then
        
    Dim dbl As Double = StrNum
    End If
     
  15. LucaMs

    LucaMs Expert Licensed User

    Because it does not work if the decimal format uses the comma, like "123,78"
     
    Last edited: May 17, 2015
  16. b4auser1

    b4auser1 Well-Known Member Licensed User

    Thank's for explanation.

    Does StrNumParse (based on java NumberFormat) convert numbers like: "1 000 0000,00" or "1,000,000.00" ?
    Or it is necessary to provide Format string to NumberFormat ?
     
  17. LucaMs

    LucaMs Expert Licensed User


    Try ;)
     
  18. corwin42

    corwin42 Expert Licensed User

    Just have seen your question. Isn't the AHNumeric.Parse() of the AHLocale library exactly what you are searching for?
     
    LucaMs likes this.
  19. LucaMs

    LucaMs Expert Licensed User

    Yes, but I found it after :) (more exactly, someone found it and indicated it to me)

    Thank you.

    I'm still curious about why the inline java code does not work.
     
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