简体   繁体   English

将字符串号格式化为双精度变量,小数点后有两个数字

[英]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 . 我从服务器获取一个字符串格式,其格式如下: 14.5000我需要从它创建一个双精度变量,其小数点后有两个数字: 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 . 我得到14.5 If I use DecimalFormat("#.00") , it gives me 15 ... 如果我使用DecimalFormat(“#。00”) ,它给我15 ...

Someone could help me with that ? 有人可以帮我吗?

If you want string with precision upto 2 points after decimal you should use 如果您想要精确到小数点后2分的字符串,则应使用

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

you have used "#,00" 您已使用“#,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 有关更多信息,请参见DecimalFormat的Java文档: 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"); 我认为。最好的答案是DecimalFormat df = new DecimalFormat("#0.00"); for you in link 在链接中为您

[ how to convert double to 2 number after the dot? [ 如何在点后将double转换为2?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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