The most complete BigDecimal's newScale (reserve decimal places) and roundingMode (rounding mode) in the whole network, introduce roundingMode in detail (key point)

Generally, when we want to reserve the decimal place and set the rounding mode for a BigDecimal number, we use it like this:
For the numbers obtained by addition, subtraction, and multiplication, directly use the BigDecimal object.setScale, like this:

 Or division, when doing divide, specify newScale and roundingMode

 For scale, it is to keep a few decimal places, so there is no need to explain it.

The following focuses on roundingMode (rounding mode). There are 8 rounding modes, and their values ​​are from 0 to 7, as follows:

ROUND_UP: 0; // round away from zero


ROUND_DOWN: 1; // round towards zero


ROUND_CEILING: 2; // round towards positive infinity


ROUND_FLOOR: 3; // round towards negative infinity


ROUND_HALF_UP: 4; // Round to the nearest number, if the distance from two adjacent numbers is equal, then round up (that is, our most commonly used rounding)


ROUND_HALF_DOWN: 5; // round towards the nearest number, if the distance from two adjacent numbers is equal, round down


ROUND_HALF_EVEN: 6; // Round towards the nearest number, if the distance from two adjacent numbers is equal, then round towards the adjacent even number


ROUND_UNNECESSARY: 7; // Used to assert that the requested operation has an exact result, so no rounding occurs

For example, for rounding, you can use BigDecimal.ROUND_HALF_UP, or you can directly use 4 instead. for example:


ROUND_UP
public static final int ROUND_UP The rounding mode for rounding away from zero. Always increment numbers before discarding non-zero parts. Note that this rounding mode never reduces the size of the computed value. 

--------------------------------------------------------------------------------

ROUND_DOWN
public static final int ROUND_DOWN rounding mode near zero. Always don't increment the number (i.e. truncate) until some part is discarded. Note that this rounding mode never increases the size of the computed value. 

--------------------------------------------------------------------------------

ROUND_CEILING
public static final int ROUND_CEILING rounding mode close to positive infinity. If the BigDecimal is positive, the rounding behavior is the same as ROUND_UP; if negative, the rounding behavior is the same as ROUND_DOWN. Note that this rounding mode never reduces the calculated value. 


--------------------------------------------------------------------------------

ROUND_FLOOR
public static final int ROUND_FLOOR The rounding mode for approaching negative infinity. If the BigDecimal is positive, the rounding behavior is the same as ROUND_DOWN; if negative, the rounding behavior is the same as ROUND_UP. Note that this rounding mode never increases the calculated value. 


--------------------------------------------------------------------------------

ROUND_HALF_UP
public static final int ROUND_HALF_UP Rounds towards the "nearest" number, or rounds up if the distance to two adjacent numbers is equal. If the discarded fraction is >= 0.5, the rounding behavior is the same as ROUND_UP; otherwise, the rounding behavior is the same as ROUND_DOWN. Note, this is the rounding mode most of us learned in elementary school. 

--------------------------------------------------------------------------------

ROUND_HALF_DOWN
public static final int ROUND_HALF_DOWN Rounds towards the "nearest" number, or rounds up if the distance to two adjacent numbers is equal. If the fraction > 0.5, the rounding behavior is the same as ROUND_UP; otherwise the rounding behavior is the same as ROUND_DOWN. 

--------------------------------------------------------------------------------

ROUND_HALF_EVEN
public static final int ROUND_HALF_EVEN rounds toward the "nearest" number, or toward the adjacent even number if the distance to two adjacent numbers is equal. If the number to the left of the discarded part is odd, the rounding behavior is the same as ROUND_HALF_UP; if it is even, the rounding behavior is the same as ROUND_HALF_DOWN. Note that this rounding mode minimizes accumulation errors when repeating a series of calculations. 


--------------------------------------------------------------------------------

ROUND_UNNECESSARY
public static final int ROUND_UNNECESSARY Asserts that the requested operation has an exact result, so no rounding is required. An ArithmeticException is thrown if this rounding mode is specified for an operation that obtains an exact result

ROUND_UP = 0

Definition: Round away from zero.

Explanation: Always add 1 to the number preceding the non-zero discarded part. Note that this rounding mode never reduces the absolute value of the calculated value.

Graphic:

Example:

type in data Rounds the input number to a single digit using UP rounding mode
5.5 6
2.5 3
1.6 2
1.1 2
1.0 1
-1.0 -1
-1.1 -2
-1.6 -2
-2.5 -3
-5.5 -6

ROUND_DOWN = 1

Definition: Round towards zero.

Explanation: Never add 1 to the number preceding the discarded part (i.e. truncate). Note that this rounding mode never increases the absolute value of the calculated value.

Graphic:

Example:

type in data Rounds the input number to a single digit using the DOWN rounding mode
5.5 5
2.5 2
1.6 1
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -1
-2.5 -2
-5.5 -5

ROUND_CEILING = 2

Definition: Rounds towards positive infinity.

Explanation: If the result is positive, the rounding behavior is similar to RoundingMode.UP; if the result is negative, the rounding behavior is similar to RoundingMode.DOWN. Note that this rounding mode never reduces the calculated value.

Graphic:

Example:

type in data Rounds the input number to a single digit using the DOWN rounding mode
5.5 6
2.5 3
1.6 2
1.1 2
1.0 1
-1.0 -1
-1.1 -1
-1.6 -1
-2.5 -2
-5.5 -5

ROUND_FLOOR = 3

Definition: Rounds towards negative infinity.

Explanation: If the result is positive, the rounding behavior is similar to RoundingMode.DOWN; if the result is negative, the rounding behavior is similar to RoundingMode.UP. Note that this rounding mode never increases the calculated value.

Graphic:

Example:

type in data Rounds the input number to a single digit using the DOWN rounding mode
5.5 5
2.5 2
1.6 1
1.1 1
1.0 1
-1.0 -1
-1.1 -2
-1.6 -2
-2.5 -3
-5.5 -6

ROUND_HALF_UP = 4 (round up)

Definition: Rounds toward the nearest number, and rounds up if two adjacent numbers are equidistant from each other.

Explanation: If the discarded part >= 0.5, the rounding behavior is the same as RoundingMode.UP; otherwise, the rounding behavior is the same as RoundingMode.DOWN. Note that this rounding mode is the rounding that is usually taught in schools.

Graphic:

Example:

type in data Rounds the input number to a single digit using the DOWN rounding mode
5.5 6
2.5 3
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -3
-5.5 -6

ROUND_HALF_DOWN = 5

Definition: Rounds toward the nearest number, or rounds down if two adjacent numbers are equidistant from each other.

Explanation: If the discarded part > 0.5, the rounding behavior is the same as RoundingMode.UP; otherwise, the rounding behavior is the same as RoundingMode.DOWN. Note that this rounding mode is what is commonly referred to as rounding.

Graphic:

Example:

type in data Rounds the input number to a single digit using the DOWN rounding mode
5.5 5
2.5 2
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -2
-5.5 -5

ROUND_HALF_EVEN = 6

Definition: Round towards the nearest number, if the distance to two adjacent numbers is equal, then round towards the adjacent even number.

解释:如果舍弃部分左边的数字为奇数,则舍入行为同 RoundingMode.HALF_UP;如果为偶数,则舍入行为同RoundingMode.HALF_DOWN。注意,在重复进行一系列计算时,根据统计学,此舍入模式可以在统计上将累加错误减到最小。此舍入模式也称为“银行家舍入法”,主要在美国使用。此舍入模式类似于 Java 中对float 和double 算法使用的舍入策略。

图示:

示例:

输入数字 使用 DOWN 舍入模式将输入数字舍入为一位数
5.5 6
2.5 2
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -2
-5.5 -6

ROUND_UNNECESSARY = 7

定义:用于断言请求的操作具有精确结果,因此不发生舍入。

解释:计算结果是精确的,不需要舍入,否则抛出 ArithmeticException。

示例:

输入数字 使用 DOWN 舍入模式将输入数字舍入为一位数
5.5 抛出 ArithmeticException
2.5 抛出 ArithmeticException
1.6 抛出 ArithmeticException
1.1 抛出 ArithmeticException
1.0 1
-1.0 -1
-1.1 抛出 ArithmeticException
-1.6 抛出 ArithmeticException
-2.5 抛出 ArithmeticException
-5.5 抛出 ArithmeticException

Guess you like

Origin blog.csdn.net/u013282737/article/details/120452426