Java uses BigDecimal to solve the problem of exact calculations

Source: Internet
Author: User
Tags decimal to binary

Recently someone has sent me a math problem, such as:

After I looked at it it was very simple, but I thought for a long while to solve it. After the solution I thought of using the program to solve again, but the precise calculation of the problem is a headache.

Problem Solving Ideas:

The idea is simple, brute force can be solved, but when you write a four-for-loop you will find that you can't solve it. This allows the result to be a decimal, changing the increment to the float type, increasing by 0.1 each time.

When you run with joy, you will find that there is still no result. It is also not possible to change to a double type.

This is because the addition of the float type in Java is to convert decimal to binary and then convert the binary result back to decimal.

This is then calculated using the BigDecimal class that comes with Java.

Of course, using the BigDecimal class differs from the value type int, float, and so on.

1. Declare a BigDecimal

BigDecimal b1=new BigDecimal ("10");

This declares a reference B1 to a BigDecimal object with a value of 10. Note that the value in the constructor method of BigDecimal is of type string.

2, BigDecimal of the increase and decrease

Add

Import Java.math.bigdecimal;public class Test3 {public static void main (string[] args) {BigDecimal b1=new BigDecimal ("1.1 2 "); BigDecimal b2=new BigDecimal ("2.14"); System.out.println (B1.add (B2));}}

Operation Result: 3.26

Substract:

Import Java.math.bigdecimal;public class Test4 {public static void main (string[] args) {BigDecimal b1=new BigDecimal ("1.1 2 "); BigDecimal b2=new BigDecimal ("2.14"); System.out.println (B1.subtract (B2));}}

Operation Result:-1.02

PS: Multiply and divide also have a corresponding method, here no longer show.

3, BigDecimal for loop

Import Java.math.bigdecimal;public class Test3 {public static void main (string[] args) {for (BigDecimal b1=new BigDecimal ( "1.0"), B1.compareto (New BigDecimal ("5"))!=1;b1=b1.add (New BigDecimal ("0.1")) {System.out.println (B1);}}}

Using BigDecimal for A For loop requires the use of the CompareTo method in BigDecimal. The CompareTo method evaluates to True to return 1, otherwise 0 is returned.

4. Methods of Solving problems

Import Java.math.bigdecimal;public class Test2 {public static void main (string[] args) {for (BigDecimal a = new BigDecimal ("0"); A.compareto (New BigDecimal ("10"))! = 1; A = A.add (new BigDecimal ("0.1"))) {for (BigDecimal B = new BigDecimal ("0"), B.compareto (New BigDecimal ("ten"))! = 1; b = B.  Add (New BigDecimal ("0.1"))) {for (BigDecimal c = new BigDecimal ("0"), C.compareto (New BigDecimal ("ten"))! = 1;c = C.add (new BigDecimal ("0.1"))) {for (BigDecimal d = new BigDecimal ("0"), D.compareto (New BigDecimal ("ten"))! = 1; d = d.add (new Bigde Cimal ("0.1")) {if ((A.add (b)). Equals (New BigDecimal ("9.0")) && B.add (d). Equals (New BigDecimal ("10.0")) & & A.add (c). Equals (New BigDecimal ("10.0")) && C.subtract (d). Equals (New BigDecimal ("6.0"))) System.out.println (A + "|" + B + "|  " + C + "|" + D);}}}}}

Terror of the four-heavy bigdecimal for loop.

Running Result: 1.5|  7.5 | 8.5 | 2.5

Java uses BigDecimal to solve the problem of exact calculations

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.