Operation of Java BigDecimal

Source: Internet
Author: User

Today, let's talk about the operation of BigDecimal in Java. Because the accuracy of the double,float is not enough, the bigdecimal to be used in the commercial calculation. The BigDecimal object is created as follows:

New BigDecimal ("12.000001"); System.out.println (b);

The output result is: 12.000001;

BigDecimal can pass in a string and double when it is created, but it is best to use string, as a result, see the following code:

    New BigDecimal ("12.000001");        System.out.println (b);         New BigDecimal (12.01);        System.out.println (c);

The result of the operation is:

12.000001
12.0099999999999997868371792719699442386627197265625 you can see that the accuracy is lost when you pass in a double.

BigDecimal Other operations are as follows:

    //addition     Public StaticBigDecimal Add (String num1, String num2) {BigDecimal bd1=NewBigDecimal (NUM1); BigDecimal Bd2=NewBigDecimal (num2); returnBd1.add (BD2); }     //Subtraction     Public StaticBigDecimal Subtract (string num1, String num2) {BigDecimal bd1=NewBigDecimal (NUM1); BigDecimal Bd2=NewBigDecimal (num2); returnbd1.subtract (BD2); }     //multiplication     Public StaticBigDecimal Multiply (string num1, String num2) {BigDecimal bd1=NewBigDecimal (NUM1); BigDecimal Bd2=NewBigDecimal (num2); returnbd1.multiply (BD2); }     //Division     Public StaticBigDecimal Divide (string num1, String num2,inti) {BigDecimal bd1=NewBigDecimal (NUM1); BigDecimal Bd2=NewBigDecimal (num2); //I is the number of digits to be retained, and the bigdecimal.round_half_up represents the rounding rule        returnbd1.divide (Bd2, I, Bigdecimal.round_half_down); }

Need to emphasize the division, the third parameter is whether rounding,

Round_half_down said to meet 5 not in 1, that is 1.5->1;
Round_half_up said 5 in 1, that is 1.5->2;

But there is a need to note:
When we use Round_half_down ,
System.out.println (this. Divide ("67.75", "5", 4)); System.out.println (this. Divide ("67.75", "5", 1)); System.out.println ("-------"); System.out.println (this. Divide ("67.751", "5", 4)); System.out.println (this. Divide ("67.751", "5", 1));

The results of the operation are as follows:

13.5500
13.5
-------
13.5502
13.6
I just wonder, according to reason the second result should be 13.5 Ah! How did it become 13.6. Later, after many tests, think about it, this rounding is not based on the accuracy of the bit behind the shift with 5 to do a comparison,

Instead, the accuracy of all the data after the comparison with 5, that is to say: 13.5500 accurate to a bit, then 0.0500 and 5 compared, and 13.5502 to one, then 0.0502 and 5 compared,

It's bigger than 5, so just pick it up.



Operation of Java 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.