[英]Convert double to exaclty two decimal place in java
我想將一個文件轉換為兩個地方。我對相同的查詢有答案,但是沒有一個可以滿足我的問題。
這是我正在使用的代碼:
Double d=15.99965754;
System.out.println("Value before formatting is : "+d);
String value=new DecimalFormat("##.00").format(d);
System.out.println("The value after formatting is :" +Double.parseDouble(value));
我得到的輸出是
格式化之前的值為:15.99965754格式化之后的值為:16.0
但是我真正期待打印的是:
格式化前的值為:15.99965754格式化后的值為:16.00
無論double值是多少,我都應該將小數精確地移到兩個位置。
不解析值,僅打印
System.out.println("The value after formatting is :" + value);
獲得相同結果的另一種方法
System.out.printf("The value after formatting is : %.2f", d);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.