Describes the large number of Java classes (BigDecimal) and eight rounding modes _java

Source: Internet
Author: User
Tags rounds

1.BigDecimal Introduction

The BigDecimal consists of an arbitrary precision integer Non scale value and a 32-bit integer scale (scale). If it is zero or positive, the scale is the number of digits after the decimal point. If it is a negative number, multiply the value of the scale's scale by 10 of the negative scale power. Therefore, the value represented by BigDecimal is ( unscaledValue × 10-scale ).

Introduction of 2.BigDecimal

When using the Java programming language to develop software that requires high-precision computing of numerical values, we often use BigDecimal and BigInteger these two large classes, rather than the common,, int long float , double type, especially in dealing with floating-point data.

Let's take a look at a code demo that uses the underlying data type double to calculate and print the results:

public class MainClass {public

  static void Main (string[] args) {
    System.out.println (0.02+0.01);
    System.out.println (0.05+0.01);
  }

The results are as follows:

0.03
0.060000000000000005

The question arises, why do the data of the second kind of result come up? The root cause or our computer is binary, and binary is no way to accurately represent a floating-point number, the CPU uses the "Mantissa and Index" (scientific counting method) to express the floating-point number of the time there are certain errors. Therefore, when the data accuracy requirements are higher, still need to use the BigDecimal class, although the calculation speed slightly slower.

Use of 3.BigDecimal

There are two ways to create a BigDecimal object that have constructors and public static methods ( BigDecimal.valueOf ), and you need to be aware of two points:

1. The constructor contains two forms that use basic data types and strings as parameters, and the latter is recommended, such as: new BigDecimal(Double.valueOf(0.09)) . You can try, the System.out.println(new BigDecimal(0.06).toString()); output of the statement is: 0.059999999999999997779553950749686919152736663818359375

2, decimal print log or conversion to the basic data type, try to use the public method it provides xxxValue() , for example doubleValue() , rather than a simple roughtoString()

4.BigDecimal rounding Mode

Although the database store is a high-precision floating-point number, but often in the application of the display when it is often necessary to limit the digits of the decimal point, such as two to three decimal places, you need to use the Setscale ( int newScale, int roundingMode ) function, as the bigdecimal of the public static variables, The rounding mode (rounding mode) has more operation rules, public eight kinds, here is a description, the official documents are also introduced.

1, Round_up

Rounds to a direction away from zero. Discard the non 0 part and add a number adjacent to the non 0.

2, Round_down

Round in the direction of close to 0. Discard not 0 parts, and not 0 discard part of the adjacent number plus one, take the interception behavior.

3, Round_ceiling

Rounds in the direction of positive infinity. If a positive number, the rounding result is the same as round_up, and if a negative number, the rounding result is consistent with the Round_down. Note: This mode does not reduce the numeric size.

4, Round_floor

Rounds to a negative infinity direction. If a positive number, the rounding result is the same as Round_down, and if a negative number, the rounding result is consistent with the round_up. Note: This mode does not increase the value size.

5, Round_half_up

Rounds to the nearest number and, if the distance from two adjacent digits is equal, the rounding mode rounded up. If you discard part >= 0.5, the rounding behavior is the same as round_up; otherwise the rounding behavior is the same as Round_down. This pattern is what we often say about our "rounding".

6, Round_half_down

Rounds to the nearest number, or down rounding mode if the distance is equal to two adjacent digits. If part > 0.5 is discarded, the rounding behavior is the same as round_up; otherwise, the rounding behavior is the same as the Round_down. This model is what we often say about our "five homes six".

7, Round_half_even

Rounds to the nearest number, and adjacent even rounding if the distance from two adjacent digits is equal. If the number on the left of the part is discarded, the rounding behavior is the same as round_half_up, and if an even, the rounding behavior is the same as the Round_half_down. Note: This rounding pattern minimizes cumulative errors when a series of computations are repeated. This rounding pattern is also known as the Banker rounding method, which is used primarily in the United States. Four homes six into, five points in two cases, if the previous one is odd, then into a bit, otherwise shed.

8, Round_unnecessary

Asserts that the requested operation has an exact result, so no rounding is required. If this rounding mode is specified for an action that obtains an exact result, a arithmeticexception is thrown.

Here's an example of the numerical results in different rounding modes, preserving a decimal number:

5. Summary

The above is the Java large number class BigDecimal and eight kinds of rounding mode of the full content of the introduction, I hope the content of this article for you to learn Java can help.

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.