大家好:
   我现在又几个整数(A,B,C,D).
计算结果:
double E=Double.parseDouble(df.format(A+B+C)/D);
我想保留两位小数,在前面写了下面两条语句:
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
df.applyPattern("0.00");
可为什么得到的结果E却不是两位小数呢?请大家帮忙分析一下是什么原因。谢谢!

解决方案 »

  1.   

    double  e = 85463.5234;
    java.text.DecimalFormat df=new java.text.DecimalFormat("#0.00");
    System.out.println(df.format(e));
      

  2.   

    df.format(A+B+C)/D 小数位只有一位
      

  3.   


    double  e = 85463.50;
    java.text.DecimalFormat df=new java.text.DecimalFormat("#0.00");
    System.out.println("字符串 "+df.format(e));   //85463.50
    String str = df.format(e);
    System.out.println("双精度 "+Double.parseDouble(str));//85463.5
    System.out.println(e);  //85463.5字符串 可以输出 xxx.00
    double xxxx.00是输不出来
      

  4.   

    double E=Double.parseDouble(df.format(A+B+C)/D);
    NumberFormat nf=NumberFormat.getPercentInstance();
    nf.setMinimumFractionDigits(2);   
    System.out.println(nf.format(E));
      试   试
      

  5.   


    顶  
    其实这种东西   google一下一大把吧
      

  6.   

    z424nixnehc,用你的方法
    double   E=Double.parseDouble(df.format(A+B+C)/D); 
    报错。
      

  7.   

    java.text.DecimalFormat df=new java.text.DecimalFormat("
    #0.00");
    System.out.println(df.format((260+260+235)/26));
    输出的结果是29.00,怎么能得到29.04?
      

  8.   


    java.text.DecimalFormat df=new java.text.DecimalFormat("#0.00");
    System.out.println(df.format((260+260+235)/26.0));
      

  9.   

     int A = 260,B=260,C=235,D=26;
     NumberFormat nf = NumberFormat.getInstance();
     nf.setMaximumFractionDigits(2);
     nf.setMinimumFractionDigits(2);
     double e = A+B+C;
     System.out.println(nf.format((e)/D));
    貌似这样可以得到值为29.04,这里面要注意的是必须定义一个中间变量e = A+B+C,否则结果就为29.00,很玄乎?
      

  10.   

    随便举个例子:       
     double a = 6523.1584d;
     a = (double) (Math.round(a * 100)) / 100;
     System.out.println(a);