Double operation in Java

Source: Internet
Author: User
Tags constructor

Simple floating-point type float and double in Java are not capable of operation. It's not just Java, it's also a problem in many other programming languages. In most cases, the results of the calculation are accurate, but try a few more times (you can do a loop) to try out similar errors. Now I finally understand why we have the BCD code.

This problem is quite serious, if you have 9.999999999999 yuan, your computer will not think you can buy 10 yuan of merchandise.

A specific currency type is provided in some programming languages to handle this situation, but Java does not. Now let's see how to solve this problem.

Rounded

Our first reaction was to do rounding. The round method in the math class cannot be set to hold a few decimals, we can only do this (keep two-bit): Public double round (double value) {

Return Math.Round (value*100)/100.0;

}

Unfortunately, the code above does not work properly, pass 4.015 to this method and it will return 4.01 instead of 4.02, as we see above

4.015*100=401.49999999999994

So if we're going to do a precise rounding, we can't do anything with a simple type.

Java.text.DecimalFormat could not solve the problem:

System.out.println (New Java.text.DecimalFormat ("0.00"). Format (4.025));

Output is 4.02

BigDecimal

This principle is also mentioned in the book effective Java, where float and double can only be used for scientific calculations or engineering calculations, and we use java.math.BigDecimal in business calculations. BigDecimal There are 4 ways to make it, and we don't care about the two that we use BigInteger to build, then there are two, and they are:

BigDecimal (double val)

Translates a double into a BigDecimal.

BigDecimal (String val)

Translates the String repre sentation of a BigDecimal into a BigDecimal.

The API outlined above is fairly clear, and usually the one above is easier to use. We may not even want to use it, what is the problem? When there is a problem, it is found that there is a detailed description of the method in the above paragraph:

Note:the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal (. 1) are exactly equal to. 1, but it are actually equal to. 1000000000000000055511151231 257827021181583404541015625. This are so because. 1 cannot to represented exactly as a double (or, for that matter, as a bi nary fraction of any finite length). Thus, the Long value, being passed in to the constructor are not exactly equal to. 1, appearances nonwithstanding.

The (String) constructor, on the other hand, are perfectly predictable:new BigDecimal (". 1") is exactly equal to. 1, as one would expect. Therefore, it is generally recommended that the (String) constructor being used in preference to this one.

If we need to calculate accurately, we must use string to build BigDecimal! The example in "effective Java" is a string to make BigDecimal, but the book does not emphasize this, perhaps a small mistake.

Solution

Now we can solve the problem by using the BigDecimal and making sure to use string to make it.

But imagine, if we're going to do an addition, we need to convert two floating-point numbers to string, then enough to cause BigDecimal, call the Add method on one of them, pass in another as a parameter, and then turn the result of the operation (BigDecimal) into a floating-point number. Can you endure such a tedious process? Below we provide a tool class arith to simplify the operation. It provides the following static methods, including subtraction and rounding:

public static double add(double v1,double v2)
  public static double sub(double v1,double v2)
  public static double mul(double v1,double v2)
  public static double div(double v1,double v2)
  public static double div(double v1,double v2,int scale)
  public static double round(double v,int scale)

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.