Bryan Fok
Bryan Fok

Reputation: 3487

Java parse a string into double

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?

Upvotes: 2

Views: 212

Answers (3)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136152

you can parse it as

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

and print it as

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

Upvotes: 1

anubhava
anubhava

Reputation: 786291

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; 

Upvotes: 4

Brian
Brian

Reputation: 17329

Well...

String s = "00034800";
double d = Double.parseDouble(s) / 100.0;
System.out.println(new DecimalFormat("0.00").format(d));

Upvotes: 10

Related Questions