保留指定位数能进行四舍五入
public static void main(String[] args) {
String str ="12312323423423.123283123";
BigDecimal b = new BigDecimal(str);
//,代表分隔符
//0.后面的##代表位数 如果换成0 效果就是位数不足0补齐
DecimalFormat d1 =new DecimalFormat("#,##0.####;(#)");
System.out.println(d1.format(b));
}
不四舍五入的方法
String str ="12312323423423.123283923";
BigDecimal b = new BigDecimal(str);
//,代表分隔符
//.后面的##代表位数 如果换成0 效果就是位数不足0补齐
DecimalFormat d1 =new DecimalFormat("#,##0.######");
// 设置舍入模式
d1.setRoundingMode(RoundingMode.FLOOR);
System.out.println(d1.format(b));
取消
评论