java 保留小数点最后一位怎么做?

2025-01-01 02:52:25
推荐回答(3个)
回答1:

Double d = 3.35;
BigDecimal bd = new BigDecimal(d);
bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);//BigDecimal.ROUND_HALF_UP 是4舍5入,BigDecimal.ROUND_DOWN是舍去,BigDecimal.ROUND_FLOOR是向上取整

回答2:

  1. double b =5.1212;

  2. DecimalFormat decimalFormat = new DecimalFormat(".#");

  3. double c =Double.parseDouble(decimalFormat.format(b)) ;

回答3:

double b =5.1212; DecimalFormat decimalFormat = new DecimalFormat(".#"); double c =Double.parseDouble(decimalFormat.format(b)) ;