Java Common Calculation formula

Source: Internet
Author: User
Mainly for double type calculation
Package com.xf.utils;

Import Java.math.BigDecimal; Public abstract class Amountutil {private Amountutil () {}/** * addition operation * @param v1 * @param v 2 * @return/public static double Add (double v1, double v2) {BigDecimal B1 = new BigDecimal (DOUBL
        E.tostring (v1));
        BigDecimal b2 = new BigDecimal (double.tostring (v2));
    Return B1.add (B2). Doublevalue (); /** * Subtraction Operation * @param v1 Bing * @param v2 meiosis * @return/public static double sub (double V
        1, double v2) {BigDecimal B1 = new BigDecimal (double.tostring (v1));
        BigDecimal b2 = new BigDecimal (double.tostring (v2));
    Return B1.subtract (B2). Doublevalue (); /** * Multiplication Operation * @param v1 multiplier * @param v2 multiplier * @return */public static double Mul (Double V
        1, double v2) {BigDecimal B1 = new BigDecimal (double.tostring (v1));
        BigDecimal b2 = new BigDecimal (double.tostring (v2)); ReTurn b1.multiply (B2). Doublevalue ();
     /** * * Division operation, when the exception occurs, accurate to 2 digits after the decimal point, after the number rounded * @param v1 dividend * @param v2 divisor * @return
    */public static double div (double v1, double v2) {return div (v1, v2, 2); /** * * Division operation, when the exception occurs, by the scale parameter specified precision, the next number rounded * @param v1 dividend * @param v2 divisor * @param scal e accurate to decimal point after several * @return/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 (double.tostring (v1));
        BigDecimal b2 = new BigDecimal (double.tostring (v2));
    Return B1.divide (B2, scale, bigdecimal.round_half_up). Doublevalue (); /** * Rounded * @param v to be rounded number * @param scale decimal point retain several * @return/public static DOUBL
        E round (double v, int scale) {if (Scale < 0) {    throw new IllegalArgumentException ("The scale must be a positive integer or zero");
        } BigDecimal B = new BigDecimal (double.tostring (v));
        BigDecimal one = new BigDecimal ("1");
    Return B.divide (one, scale, bigdecimal.round_half_up). Doublevalue (); /** * To determine whether A and B are equal * @param a * @param b * @return A==b returns True, A!=B returns false */Publi
        C Static Boolean equal (double A, double b) {BigDecimal V1 = bigdecimal.valueof (a);
        BigDecimal v2 = bigdecimal.valueof (b);
        if (V1.compareto (v2) = = 0) {return true;
    return false;
     /** * To determine whether a is greater than or equal to B * @param A * @param b * @return A&gt;=b returns True, A&AMP;LT;B returns false
        */public static Boolean Greaterthanorequalto (double A, double b) {BigDecimal V1 = bigdecimal.valueof (a);
        BigDecimal v2 = bigdecimal.valueof (b);
   if (V1.compareto (v2) >= 0) {return true;     return false;
     /** * Determines whether a is greater than B * @param a * @param b * @return A&gt;b returns True, A&AMP;LT;=B returns false
        */public static Boolean bigger (double A, double b) {BigDecimal V1 = bigdecimal.valueof (a);
        BigDecimal v2 = bigdecimal.valueof (b);
        if (V1.compareto (v2) = = 1) {return true;
    return false; /** * Determines whether a is less than b * * @param a * @param b * @return A&lt;b returns True, A&AMP;GT;=B returns FAL
        SE */public static Boolean LessThan (double A, double b) {BigDecimal V1 = bigdecimal.valueof (a);
        BigDecimal v2 = bigdecimal.valueof (b);
        if (V1.compareto (v2) = = 1) {return true;
    return false;
        /** * rounding reserved decimal two digits * @param num * @return/public static double RoundDown (double num) {
        Return double.valueof (String.Format ("%.2f", num)); return new BigDecimal (num). Setscale (2, bigdecimal.round_half_up). Doublevalue ();
        public static void Main (string[] args) {Double a = 101.005D;
        Double B = 0.0D;
        Double s = amountutil.sub (A, b);
        System.out.println (Amountutil.rounddown (s));
    System.out.println (Amountutil.div (101.1D, 1 D, 2));
 }
}

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.