前言:
在日常项目开发中,接触小数是常有的事情,而产品对于小数的处理,在不同的业务中有不同的定义,比如四舍五入、截取、向上舍位、向下舍位等等场景,面对如此多的场景,你是否会束手无策?或者说自己去研究怎么实现?其实 Java 已经给我们提供了一个枚举类 RoundingMode,RoundingMode 共有 8 种舍位方式,本篇我们来逐个分析。
RoundingMode 枚举类源码如下:
package java.math;
/**
*
* @see BigDecimal
* @see MathContext
* @author Josh Bloch
* @author Mike Cowlishaw
* @author Joseph D. Darcy
* @since 1.5
*/
public enum RoundingMode {
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a non-zero discarded fraction. Note that this
* rounding mode never decreases the magnitude of the calculated
* value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode UP Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code UP} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>3</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>2</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-2</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-3</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
UP(BigDecimal.ROUND_UP),
/**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode DOWN Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code DOWN} rounding
*<tr align=right><td>5.5</td> <td>5</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>1</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-1</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-5</td>
*</table>
*/
DOWN(BigDecimal.ROUND_DOWN),
/**
* Rounding mode to round towards positive infinity. If the
* result is positive, behaves as for {@code RoundingMode.UP};
* if negative, behaves as for {@code RoundingMode.DOWN}. Note
* that this rounding mode never decreases the calculated value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode CEILING Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code CEILING} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>3</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>2</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-1</td>
*<tr align=right><td>-1.6</td> <td>-1</td>
*<tr align=right><td>-2.5</td> <td>-2</td>
*<tr align=right><td>-5.5</td> <td>-5</td>
*</table>
*/
CEILING(BigDecimal.ROUND_CEILING),
/**
* Rounding mode to round towards negative infinity. If the
* result is positive, behave as for {@code RoundingMode.DOWN};
* if negative, behave as for {@code RoundingMode.UP}. Note that
* this rounding mode never increases the calculated value.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode FLOOR Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code FLOOR} rounding
*<tr align=right><td>5.5</td> <td>5</td>
*<tr align=right><td>2.5</td> <td>2</td>
*<tr align=right><td>1.6</td> <td>1</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<tr align=right><td>-1.0</td> <td>-1</td>
*<tr align=right><td>-1.1</td> <td>-2</td>
*<tr align=right><td>-1.6</td> <td>-2</td>
*<tr align=right><td>-2.5</td> <td>-3</td>
*<tr align=right><td>-5.5</td> <td>-6</td>
*</table>
*/
FLOOR(BigDecimal.ROUND_FLOOR),
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* Behaves as for {@code RoundingMode.UP} if the discarded
* fraction is ≥ 0.5; otherwise, behaves as for
* {@code RoundingMode.DOWN}. Note that this is the rounding
* mode commonly taught at school.
*
*<p>Example:
*<table border>
* <caption><b>Rounding mode HALF_UP Examples</b></caption>
*<tr valign=top><th>Input Number</th>
* <th>Input rounded to one digit<br> with {@code HALF_UP} rounding
*<tr align=right><td>5.5</td> <td>6</td>
*<tr align=right><td>2.5</td> <td>3</td>
*<tr align=right><td>1.6</td> <td>2</td>
*<tr align=right><td>1.1</td> <td>1</td>
*<tr align=right><td>1.0</td> <td>1</td>
*<t