A class with exact computations in Java BigDecimal
Last Update:2017-02-28
Source: Internet
Author: User
This class really works. Find the big one on the Internet, written by a female Java programmer. Awesome ~~~~~~ haha
/*
* Creation Date 2004-10-14
*
* If you need to calculate accurately, you must use string to build BigDecimal not
*/
package Com.lims.actions.testqc.comm;
/**
* @author Jstar
*
*
* Windows > Preferences > Java > Code Generation > Code and Comments
*/
import Java.math.BigDecimal;
/**
* Because Java's simple type does not accurately operate on floating-point numbers, this tool class provides fine
* Accurate floating-point number operations, including subtraction and rounding.
*/
public class Arith {
//Default division operation Precision
private static final int def_div_scale = 10;
//This class cannot be instantiated
Private Arith () {
}
/**
* Provides an accurate addition operation.
* @param v1 summand
* @param v2 Addends
* @return two parameters and
*/
public static double Add (double v1, double v2) {
BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
return B1.add (B2). Doublevalue ();
}
/**
* Provides an accurate subtraction operation.
* @param v1
* @param v2 meiosis
* @return Difference
of two parameters
*/
public static double sub (double V1, double v2) {
BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
return B1.subtract (B2). Doublevalue ();
}
/**
* Provides an accurate multiplication operation.
* @param v1 by multiplier
* @param v2 multiplier
* @return The product
of two parameters
*/
public static Double Mul (double v1, double v2) {
BigDecimal B1 = new BigDecimal (double.tostring (v1));
BigDecimal b2 = new BigDecimal (double.tostring (v2));
return b1.multiply (B2). Doublevalue ();
}
/**
* provides (relative) accurate division operations, which are accurate to
when there is an endless situation
* After the decimal point 10 digits, after the number is rounded.
* @param v1 dividend
* @param v2 divisor
* @return Two-parameter quotient
*/
public static double div (double v1, double v2) {
return Div (v1, v2, Def_div_scale);
}
/**
* Provides a (relative) precise division operation. When there is an exception, the scale parameter refers to the
* Precision, the next number is rounded.
* @param v1 dividend
* @param v2 divisor
* @param scale says it needs to be accurate to several after the decimal point.
* @return Two-parameter quotient
*/
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 ();
}
/**
* provides accurate decimal-bit rounding processing.
* @param v need rounded digital
* @param scale the decimal point and retain several
* @return Rounded results
*/
public static double 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 ();
}
};