看错了,是java.text.DecimalFormat

解决方案 »

  1.   

    Math.round()是返回int或long的,不合要求DecimalFormat请按我的要求写个例子
      

  2.   

    int temp = 2;//所取位数
    double d = 123.356;
    long l = Math.round(d*Math.pow(10,temp));
    double result = l/Math.pow(10,temp);
    System.out.println(result);
      

  3.   

    import java.math.BigDecimal;double d = 44.32212;
    double f = 33.32764;d = (new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP)).doubleValue();
    f = (new BigDecimal(f).setScale(2, BigDecimal.ROUND_HALF_UP)).doubleValue();试试这样吧,用BigDecimal来实现代价很低(方便).BTW,给你做了一个example:
    import java.math.BigDecimal;
    public class Test {
            public static void main(String[] args) {
                    double d = 44.32212;
                    double f = 33.32764;
     
                    d = (new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP)).doubleValue();
                    f = (new BigDecimal(f).setScale(2, BigDecimal.ROUND_HALF_UP)).doubleValue();
                    System.out.println(d);
                    System.out.println(f);
            }
    }
    运行结果:
    [aries@aries aries]$ java Test
    44.32
    33.33
    符合你的要求.
      

  4.   

    没有必要使用java.text.DecimalFormat
    java.math.BigDecimal已经为你实现了四舍五入的功能.