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