double 保留两位方法介绍
来源:百度文库 编辑:神马文学网 时间:2024/05/01 20:27:43
//测试double保留两位小数方法一
double d1= 1.5678;
d1=(Math.round(d1*100));
d1/=100;
System.out.println(d1);
//==方法二
BigDecimal bd = new BigDecimal(1.235543);
bd = bd.setScale(2,BigDecimal.ROUND_HALF_EVEN);
double d = bd.doubleValue();
System.out.println(d);
//==方法三
double x = 1.3355;
java.text.NumberFormat formate = java.text.NumberFormat.getNumberInstance();
formate.setMaximumFractionDigits(2);//设定小数最大为数 ,那么显示的最后会四舍五入的
String m = formate.format(x);
System.out.println(m);
//==方法四
DecimalFormat decfmt = new DecimalFormat("0.00");
System.out.println(decfmt.format(2.33782222));
double d1= 1.5678;
d1=(Math.round(d1*100));
d1/=100;
System.out.println(d1);
//==方法二
BigDecimal bd = new BigDecimal(1.235543);
bd = bd.setScale(2,BigDecimal.ROUND_HALF_EVEN);
double d = bd.doubleValue();
System.out.println(d);
//==方法三
double x = 1.3355;
java.text.NumberFormat formate = java.text.NumberFormat.getNumberInstance();
formate.setMaximumFractionDigits(2);//设定小数最大为数 ,那么显示的最后会四舍五入的
String m = formate.format(x);
System.out.println(m);
//==方法四
DecimalFormat decfmt = new DecimalFormat("0.00");
System.out.println(decfmt.format(2.33782222));