日期:2014-05-20  浏览次数:20687 次

Double精度问题【很基础的难题】
Java code

Double d = 120.00D;
System.out.println(d);
//输出结果:120.0
//================================
String s="123";
Double d=Double.valueOf(s);
System.out.println(d);
//输出结果:123.0





如果现在必须要两位小数(例如:12:00)如何处理?
求多种方法!

NumberFormat,strictfp等等!

------解决方案--------------------
最后一句改成:
Java code
System.out.println(new DecimalFormat("#.00").format(d));

------解决方案--------------------
Java code

      DecimalFormat nf = new DecimalFormat(".00");
      String s="123";
      Double d=Double.valueOf(s);
      System.out.println(nf.format(d));

------解决方案--------------------
Java code
System.out.printf("%.2f\n", d);
如果要转成String
String s = String.format("%.2f", d);

------解决方案--------------------
难得见你发帖!
------解决方案--------------------
double没有所谓2位小数的概念。

你可以將某一double格式化成具体的精度展示方式,但不能指定某一个double在一个scale下计算。

如果需要使用精度计算,请使用BigDecimal