2

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?

5
  • 3
    Double.parseDouble("00034800") / 100 ? Commented Aug 27, 2013 at 5:48
  • Are the last two digits always decimal point values? Commented Aug 27, 2013 at 5:49
  • @Aashray Yes, they are Commented Aug 27, 2013 at 5:49
  • 1
    @mshsayem You need 100.0 or you'll get the truncated value if it's something like "00034856" Commented Aug 27, 2013 at 5:49
  • Then @Brain's answer is what you are looking for. Commented Aug 27, 2013 at 5:50

3 Answers 3

10

Well...

String s = "00034800";
double d = Double.parseDouble(s) / 100.0;
System.out.println(new DecimalFormat("0.00").format(d));
Sign up to request clarification or add additional context in comments.

1 Comment

You may also like to catch NumberFormatException if you are not using a static string, ie from input or such
4

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

1

you can parse it as

    double d = Double.parseDouble("00034800") / 100;

and print it as

System.out.printf("%.2f", d);

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.