`

double处理

    博客分类:
  • Java
阅读更多
import java.text.DecimalFormat;
import java.math.BigDecimal;

public class Test_Double{
    public static void main(String[] args){
        //-----方法1--------四舍五入  round对负数是五舍六入
        double d_1 = 123.9;
        System.out.println("d_1 = "+Math.round(d_1));
        //-------方法2------------------
        DecimalFormat decfmt = new DecimalFormat("##0.00");   
          System.out.println(decfmt.format(1.33482222));
          //--------方法3--------------
          double x = 1.33345;   
        java.text.NumberFormat formate = java.text.NumberFormat.getNumberInstance();   
        formate.setMaximumFractionDigits(3);//设定小数最大为数   ,那么显示的最后会四舍五入的   
        String m = formate.format(x);   
        System.out.println(m);  
        //--------方法4--------------
        BigDecimal bd = new BigDecimal(1.234543);   
        bd = bd.setScale(3,BigDecimal.ROUND_HALF_EVEN);   
        double d = bd.doubleValue();   
        System.out.println(d);
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics