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等等!

解决方案 »

  1.   

    最后一句改成:
    System.out.println(new DecimalFormat("#.00").format(d));
      

  2.   


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

  3.   

    System.out.printf("%.2f\n", d);
    如果要转成String
    String s = String.format("%.2f", d);
      

  4.   

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

  5.   

    朋友的问题,要求返回一个两位小数的double类型,而不是string
      

  6.   

    double没有所谓2位小数的概念。你可以將某一double格式化成具体的精度展示方式,但不能指定某一个double在一个scale下计算。如果需要使用精度计算,请使用BigDecimal