Android Question [SOLVED] NumberFormat - Parse

LucaMs

Expert
Licensed User
Longtime 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
 

DonManfred

Expert
Licensed User
Longtime User
have you added the import?

B4X:
import java.text.NumberFormat;
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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:
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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:
Upvote 0

DonManfred

Expert
Licensed User
Longtime 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:
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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"?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime 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
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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).
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime 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
 
Upvote 0

b4auser1

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

b4auser1

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

corwin42

Expert
Licensed User
Longtime User
Upvote 0
Top