Android Question [SOLVED] NumberFormat - Parse

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:

B4X:
#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?

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

Thank you
 

LucaMs

Expert
Licensed User
No.

Thank you, Speedy.

Now I have to import something else because the error now is on Locale.
 
Last edited:

LucaMs

Expert
Licensed User
Uhm...

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

#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.
B4X:
'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:

LucaMs

Expert
Licensed User
I can't find the error!

The code should be simply:
B4X:
#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:
B4X:
java:437: error: unreported exception ParseException; must be caught or declared to be thrown
   return NumberFormat.getInstance().parse(Value);
                                          ^
 
Last edited:

DonManfred

Expert
Licensed User
public Number NumberParse(String value)
B4A does not know a Object Number

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

try something like this

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

LucaMs

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

B4X:
    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");
        }
    }

and what should I pass as "final BA ba"?
 

DonManfred

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

try this

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

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).
 

LucaMs

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

try this

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

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

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

LucaMs

Expert
Licensed User
B4X:
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.
 

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:
B4X:
public Integer NumParse() {
NumberFormat nf = NumberFormat.getInstance();
Number number = nf.parse("1123.12");
return 0;
}
same error, Parse is not recognized.
 

LucaMs

Expert
Licensed User
Well, I found a solution using Reflection instead of inline Java:
B4X:
Public Sub StrNumParse(StrNum As String) As Double
    Dim rfl As Reflector
    Dim nf As Object = rfl.RunStaticMethod("java.text.NumberFormat", "getInstance", Null, Null)
    rfl.Target = nf
    Dim dbl As Double = rfl.RunMethod2("parse", StrNum, "java.lang.String")
    Return dbl
End Sub
 

b4auser1

Well-Known Member
Licensed User
Why not to use auto conversion ?
B4X:
Dim StrNum As String = "123.78"
If IsNumber(StrNum) Then
    Dim dbl As Double = StrNum
End If
 

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 ?
 

corwin42

Expert
Licensed User
Top