@Test
public void quzheng(){
double f = 1.2253322;
System.out.println("BigDecimal转换");
BigDecimal bg = new BigDecimal(f);
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(f1);
System.out.println("DecimalFormat转换最简便");
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(f));
System.out.println("String.format打印最简便");
String stringf = String.format("%.2f", f);
System.out.println(stringf);
/**
* 向上取整:Math.ceil() //只要有小数都+1
* 向下取整:Math.floor() //不取小数
* 四舍五入:Math.round() //四舍五入
*/
System.out.println("向上取整、向下取整以及四舍五入");
System.out.println(Math.ceil(f));
System.out.println(Math.floor(f));
System.out.println(Math.round(f));
}
java保留小数位数的方法以及取整的方法
猜你喜欢
转载自blog.csdn.net/weixin_38690150/article/details/80208832
今日推荐
周排行