What are the 8 rounding modes for BigDecimal in Java?

Source: Internet
Author: User
Tags rounds

What are the 8 rounding modes for BigDecimal in Java? The following Changsha Obertek Software Institute and everyone together to learn it:

Java.math.BigDecimal

Immutable, arbitrary-precision, signed decimal number. The BigDecimal consists of an integer non-scaling value of arbitrary precision and a 32-bit integer scale (scales).

If zero or positive, the scale is the number of digits after the decimal point. If it is negative, the non-scaling value of the number is multiplied by the 10 negative scale power.

Therefore, the value represented by BigDecimal is (Unscaledvaluex10-scale).

There are two other classes associated with it:

Java.math.MathContext:

The object is an immutable object that encapsulates the context setting, which describes some rules of the numeric operator, such as the precision of the data, the rounding method, and so on.

Java.math.RoundingMode:

This is an enumeration type that defines a number of commonly used data rounding methods.

This class is still quite complicated, because of the rounding pattern, too many rules of data operation,

People who are not math majors look at the Chinese API, which is hard to understand when used in practice.

BigDecimal provides accurate numerical calculations in the areas of banking, accounts, and billing. 8 of these rounding methods are worth mastering.

1, Round_up

Rounds the rounding mode away from zero.

Always increase the number before discarding the non-0 part (always add 1 to the number preceding the non-0 discard part).

Note that this rounding mode always does not reduce the size of the computed value.

2, Round_down

Rounding mode of close to 0.

The number is never incremented until a part is discarded (the number preceded by the discard part is incremented by 1, i.e. truncated).

Note that this rounding mode always does not increase the size of the computed value.

3, Round_ceiling

Rounding mode that is close to positive infinity.

If 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 always does not reduce the calculated value.

4, Round_floor

Rounding mode that is close to negative infinity.

If 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 always does not increase the calculated value.

5, Round_half_up

Rounds the nearest number to the rounding mode that is rounded up if the distance to the two adjacent numbers is equal.

If part >= 0.5 is discarded, the rounding behavior is the same as round_up, otherwise the rounding behavior is the same as Round_down.

Note that this is the rounding pattern that most of us have learned in primary school (rounded).

6, Round_half_down

Rounds the nearest number to the rounding mode that is rounded up if the distance to the two adjacent numbers is equal.

If part > 0.5 is discarded, the rounding behavior is the same as round_up, otherwise the rounding behavior is the same as Round_down (five-six in).

7, Round_half_even

Rounds to the nearest number, rounded to the adjacent even if the distance to the two adjacent numbers is equal.

If the left part of the discard number is odd, the rounding behavior is the same as round_half_up;

If an even number, the rounding behavior is the same as Round_half_down.

Note that this rounding mode minimizes cumulative errors when a series of calculations are repeated.

This rounding mode is also known as the Banker rounding method, mainly used in the United States. Four homes six in, five points two cases.

If the previous bit is odd, then enter the position, otherwise give up.

The following example is the result of holding the decimal point 1 bits, then this rounding method.

1.15>1.2 1.25>1.2

8, Round_unnecessary

Asserts that the requested operation has precise results and therefore does not require rounding.

If this rounding mode is specified for operations that obtain precise results, the arithmeticexception is thrown.

What are the 8 rounding modes for BigDecimal in Java?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.