BigDecimal的round模式 转载其他博主 ,我备忘,这个不是我写的,写的很好

 

 

BigDecimal round_down round_half_up等

mark round mode:

- BigDecimal.ROUND_DOWN

截端操作,类似truncate 该模式永远不会增加被操作的数的值
  •  

- BigDecimal.ROUND_UP

在精度最后一位加一个单位  setScale(2,BigDecimal.ROUND_UP) 1.666 ->1.67  
1.011->1.02  1.010->1.01  该模式永远不会减少被操作的数的值

- BigDecimal.ROUND_CEILING

朝正无穷方向round 如果为正数,行为和round_up一样,如果为负数,行为和round_down一样
  •  

- BigDecimal.ROUND_FLOOR

朝负无穷方向round 如果为正数,行为和round_down一样,如果为负数,行为和round_up一样
  •  

- BigDecimal.ROUND_HALF_UP

Rounding mode to round towards {@literal "nearest neighbor"}
 * unless both neighbors are equidistant, in which case round up.
 * Behaves as for {@code ROUND_UP} if the discarded fraction is
 * ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}.  Note
 * that this is the rounding mode that most of us were taught in
 * grade school.
遇到.5的情况时往上近似,例: 1.5 ->;2

- BigDecimal.ROUND_HALF_DOWN

/**
 * Rounding mode to round towards {@literal "nearest neighbor"}
 * unless both neighbors are equidistant, in which case round
 * down.  Behaves as for {@code ROUND_UP} if the discarded
 * fraction is {@literal >} 0.5; otherwise, behaves as for
 * {@code ROUND_DOWN}.
 */
遇到.5的情况时往下近似,例: 1.5 ->;1 注:1.51->2

- BigDecimal.ROUND_HALF_EVEN

    /**
 * Rounding mode to round towards the {@literal "nearest neighbor"}
 * unless both neighbors are equidistant, in which case, round
 * towards the even neighbor.  Behaves as for
 * {@code ROUND_HALF_UP} if the digit to the left of the
 * discarded fraction is odd; behaves as for
 * {@code ROUND_HALF_DOWN} if it's even.  Note that this is the
 * rounding mode that minimizes cumulative error when applied
 * repeatedly over a sequence of calculations.
 */
如果舍弃部分左边的数字为奇数,则作   ROUND_HALF_UP   ;如果它为偶数,则作   ROUND_HALF_DOWN

猜你喜欢

转载自blog.csdn.net/zy103118/article/details/85233687