Resolving Java float double floating-point type participates in computational loss accuracy

Source: Internet
Author: User
Tags float double

I used to do a community e-commerce application, found a sinkhole ....... ..... Make me cry. Below listen to my pit road, e-commerce must deal with ¥, calculate the indispensable. Because I am too simple to think that float double can directly participate in subtraction feel this code foolproof not to think so many direct float*int. Before the simple test is not a problem, the project on the line after their own accidental discovery (do not know what the company testing), when the choice of more than one product, or directly say the phenomenon, such as in the code 0.1 f * A A is an int type when A = 1 The result is correct but slowly increase to a = 9 of the time the result equals 0.9000000. (how many 0 forgot) ... 4, it was embarrassing when a face was forced later Baidu looked at the floating-point accuracy is not what, anyway, I this rookie does not move, but others have a solution this is the focus .... Therefore, do not arbitrarily take the floating-point type for the operation, especially the amount involved. Do not say I put the arithmetic of the tool class to paste out, while the boss has not found hurriedly updated.

The tool classes are as follows:

/**
* @author Mr_peng
* @created at 2017/6/9 10:13.
* @describe: Java precision Operations
*/

public class Arithutil {
private static final int def_div_scale=10;

Private Arithutil () {}
public static double Add (double d1,double D2) {
BigDecimal b1=new BigDecimal (double.tostring (D1));
BigDecimal b2=new BigDecimal (double.tostring (D2));
Return B1.add (B2). Doublevalue ();

}

public static double sub (double d1,double D2) {
BigDecimal b1=new BigDecimal (double.tostring (D1));
BigDecimal b2=new BigDecimal (double.tostring (D2));
Return B1.subtract (B2). Doublevalue ();

}

public static double Mul (double d1,double D2) {
BigDecimal b1=new BigDecimal (double.tostring (D1));
BigDecimal b2=new BigDecimal (double.tostring (D2));
Return b1.multiply (B2). Doublevalue ();

}

public static double div (double d1,double D2) {

Return Div (D1,d2,def_div_scale);

}

public static double div (double d1,double d2,int scale) {
if (scale<0) {
throw new IllegalArgumentException ("The scale must is a positive integer or zero");
}
BigDecimal b1=new BigDecimal (double.tostring (D1));
BigDecimal b2=new BigDecimal (double.tostring (D2));
Return B1.divide (B2,scale, bigdecimal.round_half_up). Doublevalue ();

}
}

Proof: True and effective, the result of the operation 100% is accurate does not appear the extra decimal digits.

Resolving Java float double floating-point type participates in computational loss accuracy

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.