Finance Project Java Development _bigdecimal (solving computational accuracy issues)

Source: Internet
Author: User

When a double is used for business operations, the double calculation loses precision. You can use BigDecimal for calculations.

[Java]View PlainCopyPrint?
  1. Import  Java.math.BigDecimal;
  2. Import  org.junit.Test;
  3. Public class testbigdecimal{
  4. @Test   
  5. Public void Test () {
  6. Double a= 0.1  ;
  7. Double b= 0.2  ;
  8. System.out.println (A+B);
  9. BigDecimal a1=New BigDecimal ("0.1");
  10. BigDecimal b1=New BigDecimal ("0.2");
  11. System.out.println (A1+B1);
  12. BigDecimal c1=New BigDecimal ("0.23574");
  13. A1=a1.add (C1);
  14. A1=a1.setscale (1, Bigdecimal.round_down);
  16. }
  17. }
Import Java.math.bigdecimal;import Org.junit.test;public class testbigdecimal{  @Test public  void Test () {      double a=0.1;      Double b=0.2;      System.out.println (a+b);      BigDecimal a1=new BigDecimal ("0.1");      BigDecimal b1=new BigDecimal ("0.2");      System.out.println (A1+B1);//Do not write      System.out.println (A1.add (B1));//correct      BigDecimal c1=new BigDecimal (" 0.23574 ");      A1=a1.add (C1);      A1=a1.setscale (1,bigdecimal.round_down);      SYSTEM.OUT.PRINTLN (A1);  }}

The output results are as follows:



The addition and subtraction of double cannot accurately calculate 0.3, while using BigDecimal can.

Of course, if you directly pass double to BigDecimal, you will find that not only can not solve the accuracy problem, but the accuracy of the completion. So, to ensure accuracy, we pass the string to it.

The Bigdecimal.setscale () method is used to format the decimal point
Setscale (1) indicates that a decimal number is reserved and is rounded by default
Setscale (1,bigdecimal.round_down) directly removes the extra decimal digits, such as 2.35 will become 2.3
Setscale (1,BIGDECIMAL.ROUND_UP) Carry processing, 2.35 becomes 2.4
Setscale (1,bigdecimal.round_half_up) rounded, 2.35 turned 2.4Setscaler (1,bigdecimal.round_half_down) rounded, 2.35 becomes 2.3, if 5 is down

. scale () precision value, that is, the number of digits after the decimal point (note: BigDecimal can improve accuracy by setscale, as long as the new value is larger than the original!)
BigDecimal can also be setscale to reduce accuracy. Because the new value is smaller than the original, so you must ensure that the original value of the bit after the decimal point is 0, only in this way can be set than the original small precision.

Example: The original value is: 4.1235648, want to set the scale to less than 7 will be wrong, if the original value is: 4.1235000, the scale is set to less than 4 bits will be wrong, and set to 4, 5, 6, 7 are no problem, set a larger, certainly will not be wrong)

Add (BigDecimal) BigDecimal the value in the object, and then returns the object.
Subtract (BigDecimal) subtracts the value from the BigDecimal object and returns the object.
Multiply (BigDecimal) multiplies the values in the BigDecimal object and returns the object.
Divide (BigDecimal) divides the values in the BigDecimal object, and then returns the object.

BigDecimal Divide (BigDecimal divisor, int scale, int roundingmode)

Example:BigDecimal mdata = new BigDecimal ("9.655"). Setscale (2, bigdecimal.round_half_up);


ToString () Converts the numeric value of the BigDecimal object to a string.
Doublevalue () returns the value in the BigDecimal object as a double-precision number.
Floatvalue () returns the value in the BigDecimal object as a single-precision number.
Longvalue () returns the value in the BigDecimal object as a long integer.
Intvalue () returns the value in the BigDecimal object as an integer.

Finance Project Java Development _bigdecimal (solving computational accuracy issues)

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: 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.