Yellow Flash
Yellow Flash

Reputation: 872

Convert "0.25000%" to double in java

Please help to convert the string value ("0.25000%") to double value.

0.25000% = 0.0025 (need to get this value as double)

String num = "0.25000%";

double d = Double.parseDouble(num);//does not work

Upvotes: 2

Views: 119

Answers (3)

Prathibha Chiranthana
Prathibha Chiranthana

Reputation: 822

String num = "0.25000%";
BigDecimal d = new BigDecimal(num .trim().replace("%","")).divide(BigDecimal.valueOf(100));//no problem BigDecimal

this can convert for decimal.

Upvotes: 2

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35577

You can try this

 String num = "0.25000%";
 double d = Double.parseDouble(num.replace("%","")); // remove %
 System.out.println(d);

Out put:

 0.25

For your edit:

You can divide final answer by 100

System.out.println(d/100);   

Now out put:

0.0025

Upvotes: 5

code monkey
code monkey

Reputation: 2124

Remove the % character and divide by 100

String num = "0.25000%";
double d = Double.parseDouble(num.replace("%","")) / 100;

Upvotes: 1

Related Questions