Subtraction of double type data in Java

Source: Internet
Author: User
Subtraction public class Arith {for double type data in Java
private static final int def_div_scale = 10;

/**
* Two double number added
* @param v1
* @param v2
* @return Double
*/
public static double Add (Double v1,double v2) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
Return B1.add (B2). Doublevalue ();
}

/**
* Subtract two double numbers
* @param v1
* @param v2
* @return Double
*/
public static double sub (double v1,double v2) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
Return B1.subtract (B2). Doublevalue ();
}

/**
* Two double number multiplied
* @param v1
* @param v2
* @return Double
*/
public static double Mul (Double v1,double v2) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
Return b1.multiply (B2). Doublevalue ();
}

/**
* Divide two double numbers
* @param v1
* @param v2
* @return Double
*/
public static double div (Double v1,double v2) {
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
Return B1.divide (B2,DEF_DIV_SCALE,BIGDECIMAL.ROUND_HALF_UP). Doublevalue ();
}

/**
* Divides two double numbers and preserves scale decimal places
* @param v1
* @param v2
* @param scale
* @return Double
*/
public static double div (double v1,double v2, int scale) {
if (Scale < 0) {
throw New IllegalArgumentException (
"The scale must be a positive integer or zero");
}
BigDecimal B1 = new BigDecimal (v1.tostring ());
BigDecimal b2 = new BigDecimal (v2.tostring ());
Return B1.divide (B2,SCALE,BIGDECIMAL.ROUND_HALF_UP). Doublevalue ();
}

}

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.