简体   繁体   中英

Formatting string number into double variable with two number after decimal point

I'm getting from server a string value formatted as follow: 14.5000 I need to create a double variable from it with two number after decimal point: 14.50 . I've tried the following:

     DecimalFormat df = new DecimalFormat("#,00");
     Double priceD = Double.parseDouble((produitParam.item(paramNb).getTextContent()));
     String dx = df.format(priceD);
     produit.setPrixTtc(Double.valueOf(dx));

And I'm getting 14.5 . If I use DecimalFormat("#.00") , it gives me 15 ...

Someone could help me with that ?

If you want string with precision upto 2 points after decimal you should use

DecimalFormat df = new DecimalFormat("#.00");

you have used "#,00"

',' is used for specifying grouping Separator.

for more information here is the Java Doc of DecimalFormat: http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

you should look this link.There is a lot of answer your question.

I think.The best answer is DecimalFormat df = new DecimalFormat("#0.00"); for you in link

[ how to convert double to 2 number after the dot?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM