Java BigDecimal Eight rounding modes

Source: Internet
Author: User
Tags rounds

I. Introduction of BigDecimal

Java.math.BigDecimal

Immutable 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).

Scaling (scale) if it is 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.

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.

Second, rounding mode

(1) round_up

Rounds the rounding mode away from zero.

Such as:

1.x rounded to 2

-1.x rounded to-2

2, Round_down

Rounding mode of close to 0.

Such as:

1.x rounded to 1

-1.x rounded to-1

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.

Such as:

1.x rounded to 2

-1.x rounded to-1

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.

Such as:

1.x rounded to 1

-1.x rounded to-2

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 xi>= 5 is discarded, the rounding behavior is the same as round_up;

The xi<5 rounding behavior is the same as Round_down.

Such as:

1.4 Rounded to 1

1.5 Rounded to 2

-1.4 Rounded to-1

-1.5 rounded to-2

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 XI > 5 is discarded, the rounding behavior is the same as round_up;

Xi <= 5 rounding behavior is the same as Round_down (five shekels six in).

1.6 Rounded to 2

1.5 Rounded to 1

-1.6 Rounded to-2

-1.5 rounded to-1

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.

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

The first one is odd, then it is in place, otherwise it is gone.

Such as:

1.6 Rounded to 2

1.5 Rounded to 2

4.5 Rounded to 4

4.4 Rounded to 4

-1.6 Rounded to-2

-1.5 rounded to-2

-6.5 rounded to-6

-6.3 Rounded to-6

8, Round_unnecessary

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

Throws a arithmeticexception if this rounding mode is specified for operations that do not have precise results.

Such as:

4.0 rounded to 4

4.1 Rounding Throw ArithmeticException

Java BigDecimal Eight rounding modes

Related Article

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.