Java BigDecimal class usage and considerations _java

Source: Internet
Author: User

BigDecimal Introduction

The JDK documentation (in Chinese) is interpreted as follows:

Immutable, arbitrary-precision signed decimal digits. 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 (Unscaledvaluex10-scale).

Specific explanation

1. "The value of the BigDecimal object is immutable". This shows this feature in the operational function of the BigDecimal object:

Copy Code code as follows:
BigDecimal a = new BigDecimal ("1.22");
System.out.println ("construct with a String value:" + a);
BigDecimal B = new BigDecimal ("2.22");
A.add (b);
System.out.println ("A plus B is:" + a);

It is easy to think that the output will be:

Construct with a stringvalue:1.22
A plus b is:3.44

But actually a plus b is:1.22

2. "BigDecimal is composed of an arbitrary precision integer Non scale value and a 32-bit integer scale (scale). If the scale value is zero or positive, the scale is the number of digits after the decimal point. This sentence can look like this:

For example:-12 and 13.412

Expressed as: -12x10-0 and 13412x10-3

Here are represented by (non scale values and scales): [-12, 0] and [13412, 3]

3. "If the scale value is negative, multiply the non scale value of the number by a negative scale of 10." This sentence can look like this:

For example: 120.00

This value is expressed as: 12000x10-2

This is represented by (non-scale values and scales), respectively: [12000, 2]

The scale value here is still positive 2, but do the following:

BigDecimal amount = new BigDecimal ("-120.00");
Returns a value that is superior to this decimal, but removes all trailing zeros from the BigDecimal.
Amount = Amount.striptrailingzeros ();

This value is expressed as: 12x10-(-1)

This is indicated by (non scale value and scale): [12,-1]

Usage considerations

1. Constructor function

Copy Code code as follows:
BigDecimal adouble =new BigDecimal (1.22);
System.out.println ("construct with a Double value:" + adouble);
BigDecimal astring = new BigDecimal ("1.22");
System.out.println ("construct with a String value:" + astring);

The output results are as follows:

Construct with a doublevalue:1.2199999999999999733546474089962430298328399658203125
Construct with a String value:1.22

Description of JDK:

A the result of the constructed method with the parameter type double is somewhat unpredictable. One might think that writing to Newbigdecimal (0.1) in Java is exactly equal to 0.1 (BigDecimal value 1 with a scale of 1). But it actually equals 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be accurately represented as a double (or, for that case, cannot be represented as any finite-length binary decimal). In this way, the value passed in to the constructed method does not exactly equal 0.1 (although it is equal to the value on the surface).

B on the other hand, the String construction method is completely predictable: writing to Newbigdecimal ("0.1") creates a BigDecimal that is exactly equal to the expected 0.1. Therefore, in comparison, it is generally recommended that you use string construction methods preferentially.

C when double must be used as a source for BigDecimal, note that this constructor provides an exact conversion; it does not provide the same result as the following: First use the double.tostring (double) method, and then use BigDecimal ( String) to construct the method. Converts a double to a string, or you can use the static method of string: String.valueof (Double).

2. Operation Operations. Subtraction actually returns a new BigDecimal object, because BigDecimal is immutable (immutable), when each step of the operation, will produce a new object, so A.add (b); Although the addition operation, But a does not save the value after the operation, the correct use should be A=a.add (b);

Example:

Determines whether the BigDecimal object is an integer:

Copy Code code as follows:
Private Boolean Isintegervalue (BigDecimal BD) {
return Bd.signum () = = 0 | | Bd.scale () <= 0 | | Bd.striptrailingzeros (). Scale () <= 0;
}

Why do you want to do this, please test the following example:

Copy Code code as follows:
BigDecimal amount = new BigDecimal ("-120.00");//Please try "0", "0.00", "1.00", "10.00" and "10.10" respectively.
System.out.println (Amount.signum ());//positive or negative
System.out.println (Amount.scale ()); Scale of
System.out.println (Amount.striptrailingzeros (). scale ());//scale after 0

Reference: Class BigDecimal

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.