Java, provides a large number (more than 16 significant bits) of the operation class, and class java.math.BigDecimal class java.math.BinInteger i.e., for high precision calculations.
Wherein BigInteger class is a class for the processing of large integers, and BigDecimal class is the class for the treatment of several sizes.
realize BigDecimal class uses BigInteger class, the difference is added to the concept of fractional BigDecimal.
float and Double can only be used for scientific computing or engineering calculations; in the commercial calculation, digital high precision, and must use the BigInteger class BigDecimal class that supports any given point precision, can use to accurately calculate the monetary value.
BigDecimal classes create an object, you can not use the traditional +, -, *, / other arithmetic operator is directly subjected to mathematical operations, and its corresponding method must be called. method parameters must BigDecimal object type.
A, common method configured BigDecimal objects
1, a method
BigDecimal BigDecimal (double d); // not allowed
2. Method Two
BigDecimal BigDecimal (String s); // common and recommended
3. Method Three
static BigDecimal valueOf (double d); // common recommended
note:
1. double constructor parameter, because it is not allowed !!!! not accurately obtain the corresponding value, the value becomes larger;
2. String constructor is completely predictable: writing new BigDecimal ( "0.1") We will create a BigDecimal, which is exactly equal to the expected 0.1; therefore, priority is generally recommended to use String constructor;
3. static method valueOf (double val) internal implementation is still a double type into a String; this is usually the double ( or float) is converted to a BigDecimal preferred method;
Test code is as follows:
package com.qiyuan.util; import java.math.BigDecimal; public class orderCode { public static void main(String[] args) { double d1 = 0.10334; double d2 = 1234.0; System.out.println ( "new BigDecimal (" + d1 + ") =" + new BigDecimal (d1)); // this way is absolutely not allowed !!!!! System.out.println ( "new BigDecimal (" + d2 + ") =" + new BigDecimal (d2)); // this way is absolutely not allowed !!!!! System.out.println(""); System.out.println("new BigDecimal(String.valueOf("+d1+"))=" + new BigDecimal(String.valueOf(d1))); System.out.println("new BigDecimal(String.valueOf("+d2+"))=" + new BigDecimal(String.valueOf(d2))); System.out.println(""); System.out.println("new BigDecimal(String.valueOf("+d1+"))=" + new BigDecimal(Double.toString(d1))); System.out.println("new BigDecimal(String.valueOf("+d2+"))=" + new BigDecimal(Double.toString(d2))); System.out.println(""); System.out.println("BigDecimal.valueOf("+d1+")=" + BigDecimal.valueOf(d1)); System.out.println("BigDecimal.valueOf("+d2+")=" + BigDecimal.valueOf(d2)); System.out.println(""); BigDecimal b1 = BigDecimal.valueOf(1); BigDecimal b2 = BigDecimal.valueOf(1.00000); System.out.println(b1.equals(b2)); System.out.println(b1.compareTo(b2)); } }
Output is as follows:
new BigDecimal(0.10334)=0.10334000000000000130118138486068346537649631500244140625 new BigDecimal(1234.0)=1234 new BigDecimal(String.valueOf(0.10334))=0.10334 new BigDecimal(String.valueOf(1234.0))=1234.0 new BigDecimal(String.valueOf(0.10334))=0.10334 new BigDecimal(String.valueOf(1234.0))=1234.0 BigDecimal.valueOf(0.10334)=0.10334 BigDecimal.valueOf(1234.0)=1234.0 false 0
Two, BigDecimal retain decimal places
public static void main(String[] args) throws IllegalAccessException { BigDecimal decimal = new BigDecimal("1.12345"); System.out.println(decimal); BigDecimal setScale = decimal.setScale(4,BigDecimal.ROUND_HALF_DOWN); System.out.println(setScale); BigDecimal setScale1 = decimal.setScale(4,BigDecimal.ROUND_HALF_UP); System.out.println(setScale1); }
Parameter definition ROUND_CEILING Rounding mode to round towards positive infinity. Rounding toward positive infinity direction ROUND_DOWN Rounding mode to round towards zero. Rounding towards zero ROUND_FLOOR Rounding mode to round towards negative infinity. To negative infinity rounding ROUND_HALF_DOWN Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. To (from) the nearest side rounding, unless (distance) on both sides are equal, and if so, rounded down, for example, one decimal result is 1.55 1.5 ROUND_HALF_EVEN Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. To (from) the nearest side rounding, unless (distance) on both sides are equal, and if so, if the reserved bits is odd, use as ROUND_HALF_UP, if it is an even number, using ROUND_HALF_DOWN ROUND_HALF_UP Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. To (from) the nearest side of rounding, unless (distance) is equal on both sides, and if so, rounded up, 1.55 to one decimal place result was 1.6 ROUND_UNNECESSARY Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. Calculations are accurate, no rounding mode ROUND_UP Rounding mode to round away from zero. In a direction away from zero rounding
Annex 1, BigDecimal class valueOf () method Source
public static BigDecimal valueOf(double val) { return new BigDecimal(Double.toString(val)); }
Annex 2, several common methods class BigDecimal
/** * Remainder number * Return value (this% divisor) of BigDecimal */ BigDecimal remainder(BigDecimal divisor); /** * Negation * The return value is (-this) of BigDecimal */ BigDecimal negate(); /** * This BigDecimal with the specified BigDecimal compare * According to this method, but the two values are equal BigDecimal objects (e.g., 2.0 and 2.00) having different scales are considered equal; * Method opposite each six boolean comparison operators (! <, ==,>,> =, =, <=) each operator, the method provides priority; * Recommended that the comparison performed using the following statements: (x.compareTo (y) <op> 0), where <op> is one of the six comparison operators; * * Returns: Comparable <BigDecimal> in compareTo * Returns: this BigDecimal is numerically less than, greater than or equal to val -1, 0, 1, or */ int compareTo(BigDecimal val);
Annex 3, provide accurate floating-point operations (including addition, subtraction, multiplication, division, rounding) source of tools
package com.qiyuan.util; import java.math.BigDecimal; public class ArithUtil { // default precision division private static final int DEF_DIV_SCALE = 10; private ArithUtil() { } /** * Precise addition */ public static double add(double value1, double value2) { BigDecimal b1 = BigDecimal.valueOf(value1); BigDecimal b2 = BigDecimal.valueOf(value2); return b1.add(b2).doubleValue(); } /** * Accurate subtraction */ public static double sub(double value1, double value2) { BigDecimal b1 = BigDecimal.valueOf(value1); BigDecimal b2 = BigDecimal.valueOf(value2); return b1.subtract(b2).doubleValue(); } /** * Precision multiplication */ public static double mul(double value1, double value2) { BigDecimal b1 = BigDecimal.valueOf(value1); BigDecimal b2 = BigDecimal.valueOf(value2); return b1.multiply(b2).doubleValue(); } /** * Exact division using default precision */ public static double div(double value1, double value2) throws IllegalAccessException { return div(value1, value2, DEF_DIV_SCALE); } /** * Precise division * @Param scale precision */ public static double div(double value1, double value2, int scale) throws IllegalAccessException { if(scale < 0) { throw new IllegalAccessException ( "accuracy not less than 0"); } BigDecimal b1 = BigDecimal.valueOf(value1); BigDecimal b2 = BigDecimal.valueOf(value2); // return b1.divide(b2, scale).doubleValue(); return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } /** * Rounding * @Param scale after several decimal places */ public static double round(double v, int scale) throws IllegalAccessException { return div(v, 1, scale); } /** * Comparison of size */ public static boolean equalTo(BigDecimal b1, BigDecimal b2) { if(b1 == null || b2 == null) { return false; } return 0 == b1.compareTo(b2); } public static void main(String[] args) throws IllegalAccessException { double value1=1.2345678912311; double value2=9.1234567890123; BigDecimal value3=new BigDecimal(Double.toString(value1)); BigDecimal value4=new BigDecimal(Double.toString(value2)); System.out.println ( "precision adder =================" + ArithUtil.add (value1, value2)); System.out.println ( "accurate subtraction =================" + ArithUtil.sub (value1, value2)); System.out.println ( "precision multiplication =================" + ArithUtil.mul (value1, value2)); System.out.println ( "exact division using default precision =================" + ArithUtil.div (value1, value2)); System.out.println ( "exact division setting precision =================" + ArithUtil.div (value1, value2,20)); System.out.println ( "decimal point is rounded off several =================" + ArithUtil.round (value1, 10)); System.out.println ( "A Comparison of =================" + ArithUtil.equalTo (value3, value4)); } }
BigDecimal add(BigDecimal augend) : 加
BigDecimal subtract(BigDecimal subtrahend) :减
BigDecimal multiply(BigDecimal multiplicand) :乘
BigDecimal divide(BigDecimal divisor):除
Arithmetic format: Return Value BigDecimal = .add BigDecimal objects (Object another BigDecimal)
E.g:
BigDecimal b72 = new BigDecimal("0.72");
BigDecimal b08 = new BigDecimal("0.08");
//加法
BigDecimal b80 = b72.add(b08);
// to solve the problem of loss of double precision
System.out.println ( "b80 =" + b80 );
//减法
BigDecimal b64 = b72.subtract(b08);
System.out.println("b64 = " + b64);
//乘法
BigDecimal bmul = b72.multiply(b08);
System.out.println("bmul = " + bmul);
//除法
BigDecimal bdiv = b72.divide(b08);
System.out.println("bdiv = " + bdiv);