Use of Java bigdecimal

Source: Internet
Author: User

First read the official documents

 
/* The results of thisconstructor can be somewhat unpredictable. one might assume that new bigdecimal (. 1) is exactlyequal. 1, but it is actually equal since this is so because.1 cannot be represented exactlyas a double (or, for that matter, as a binary fraction of any finite length ). thus, the long value that is being passed in to the constructor is not exactly equal. 1, appearances nonwithstanding. the (string) constructor, onthe other hand, is perfectly predictable: New bigdecimal (". 1 ") is exactlyequal. 1, as one wocould CT. therefore, it is generally recommended that the (string) constructor be used in preference to this one. */

 

That is to say, using double as the parameter constructor cannot accurately construct a bigdecimal object. You need to specify the context, that is, specify the exact bit. The string object can be used as the constructor passed in to accurately construct a bigdecimal object.

 
Bigdecimal B = newbigdecimal ("0.3"); // bigdecimal B = new bigdecimal (0.3); system. out. println (B. multiply (bigdecimal. valueof (100); system. out. println (B. floatvalue () * 100); system. out. println (B. multiply (bigdecimal. valueof (100 )). setscale (0, bigdecimal. round_half_up ));

 

For the setscale function:

Setscale (1) indicates that the decimal number is retained. The rounding method is used by default.

Setscale (1, bigdecimal. round_down) directly deletes unnecessary decimal places. For example, 2.35 will change to 2.3.

Setscale (1, bigdecimal. round_up) carry processing, 2.35 to 2.4

Setscale (1, bigdecimal. round_half_up) Rounding, 2.35 to 2.4

Setscaler (1, bigdecimal. round_half_down

About type conversion:

The bigdecimal class provides methods such as intvalue (), floatvalue (), doublevalue (), and longvalue () to convert a bigdecimal object to a corresponding value.

Valueof () is also provided to convert float and double types to bigdecimal

 

Summary:

1. The results of this constructor are unpredictable. Some people may think that the bigdecimal created by writing new bigdecimal (0.1) in Java is exactly equal to 0.1 (the non-standard degree value 1, its scale is 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be accurately expressed as a double (or in this case, it cannot be expressed as any binary decimal with a limited length ). In this way, the value passed into the constructor is not exactly equal to 0.1 (although it is equal to this value on the surface ).

2. On the other hand, the string constructor is completely predictable: writing new bigdecimal ("0.1") will create a bigdecimal, Which is exactly equal to the expected 0.1. Therefore, we recommend that you use the string constructor first.

3. When double must be used as the bigdecimal source, note that this constructor provides an accurate conversion. It does not provide the same results as the following operations: Double is used first. tostring (double) method, and then use the bigdecimal (string) constructor to convert double to string. To obtain this result, use the static valueof (double) method.

Supplement:

1. When bigdecimal is calculated, the result of the loop is obtained due to division, causing an exception!

Bigdecimal Total = new bigdecimal (200). setscale (3, bigdecimal. round_half_up );

Indicates that the initial value of total is 200.000.

 

Bigdecimal total2 = new bigdecimal (10). setscale (3, bigdecimal. round_half_up );

 

Indicates that the initial value of total2 is 10.000.

 

Total. Divide (total2) cannot be used for Division of two values );

 

If so, an error is reported:

 

Non-terminating decimal expansion; no exact representable decimal result

 

Correct Method:

 

Total. Divide (total2, 3, bigdecimal. round_half_up );

 

In this way, no error is reported. You must keep the decimal places for it.

 

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.