How do I parse this Double in string "00034800" into a Double value? The last 2 digits are actually decimal point, so the result I am looking for is 348.00. Is there a such format I can use with Decimal Format?
3 Answers
Well...
String s = "00034800";
double d = Double.parseDouble(s) / 100.0;
System.out.println(new DecimalFormat("0.00").format(d));
1 Comment
Adrian
You may also like to catch NumberFormatException if you are not using a static string, ie from input or such
Java Double has a constructor that takes a String.
You can do:
Double d = new Double("00034800");
And then
double myval = d.doubleValue() / 100.0;
Comments
you can parse it as
double d = Double.parseDouble("00034800") / 100;
and print it as
System.out.printf("%.2f", d);
1 Comment
Brian
You need
100.0 or you'll get the truncated value if it's something like "00034856" (how many times do I need to post this..?)
Double.parseDouble("00034800") / 100?100.0or you'll get the truncated value if it's something like"00034856"