How do I convert "formatted double (String)" to double?
I have the following code:
NumberFormat nf=NumberFormat.getInstance(Locale.FRANCE);
nf.setMinimumFractionDigits(3);
String s= nf.format(3456.32);
// double d= Double.valueOf(s); error here
System.out.println(s);
This code displays:
3 456,320
This is fine so far, but imagine, when I get the same format from JFormattedTextField, how can I convert "3 456 320" as a string to double (3456.32)?
I've already tried this:
String s="3 456,320";
double d= Double.valueOf(s);
but i get an error ...
EDIT
after a few tests i found that the code is not that reliable, e.g .:
//this works
String s="3456,6567";
NumberFormat nf1=NumberFormat.getInstance(Locale.FRANCE);
double d = (double) nf1.parse(s);
but this one:
String s="3456,0";
NumberFormat nf1=NumberFormat.getInstance(Locale.FRANCE);
double d = (double) nf1.parse(s);
I am getting this error:
Exception on thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
by cons I find this very reliable:
double d = nf1.parse(s).doubleValue();
we have to test the software several times !!!
+3
source to share