0

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 ?

Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63

2 Answers2

2

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

Abhijeet Kale
  • 1,656
  • 1
  • 16
  • 34
0

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?

Community
  • 1
  • 1
tarikfasun
  • 314
  • 6
  • 16
  • I've just figure out that it's the function **Double.parseDouble((produitParam.item(paramNb).getTextContent()));** that removes all the zeros.... is their a way to convert directly a string to double with 2 number after the decimal point ? – Hubert Solecki Sep 09 '15 at 14:51
  • DecimalFormat df = new DecimalFormat("#0.00"); String a = "14.5800"; Double priceD = Double.parseDouble(a); String dx = df.format(priceD); System.out.println(Double.valueOf(dx)); if i run this code in my machine.it work right.this function didnt removes all the zeros. – tarikfasun Sep 09 '15 at 15:04
  • It's not possible. The method **Double.parseDouble** is known for removing all zeros. (http://www.coderanch.com/t/385764/java/java/covert-String-double-loosing-precision). I've decided to use BigDecimal instead – Hubert Solecki Sep 10 '15 at 08:10