BigDecimal altogether there are 4 ways to make it, let's look at two common uses:
First type: BigDecimal (double val)
Translates a double into a BigDecimal.
Second type: BigDecimal (String val)
Translates the String repre sentation of a BigDecimal into a BigDecimal.
To use BigDecimal to create a string, to do an addition, you need to convert two floating-point number to string, and then enough to cause BigDecimal, on one of the call the Add method, passed in another as a parameter, The result of the operation (BigDecimal) is then converted to a floating-point number.
public static double Add (Double v1,double v2)
public static double sub (double v1,double v2)
public static double Mul (Double v1,double v2)
public static double div (Double v1,double v2)
public static double div (double v1,double v2,int scale)
public static double round (double v,int scale)
Basic use:
Public classarith{Private Static Final intDef_div_scale = 10;//This class cannot be instantiated PrivateArith () {}/*** provides accurate addition operations. * @paramv1 summand *@paramV2 Addend *@returntwo parameters of A and*/ Public Static DoubleAddDoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnB1.add (B2). Doublevalue (); } /*** provides accurate subtraction operations. * @paramv1 minuend *@paramv2 *@returnthe difference of two parameters*/ Public Static DoubleSubDoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnb1.subtract (B2). Doublevalue (); } /*** provides accurate multiplication operations. * @paramv1 by multiplier *@paramV2 Multiplier *@returnproduct of two parameters*/ Public Static DoubleMulDoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnb1.multiply (B2). Doublevalue (); } /*** Provide (relative) accurate division operations, when there are no more than an endless situation, accurate to * after the decimal 10 digits, after the number rounded. * @paramV1 Dividend *@paramv2 Divisor *@returntwo parameters of the quotient*/ Public Static DoubleDivDoubleV1,Doublev2) { returnDiv (V1,v2,def_div_scale); } /*** Provide (relative) accurate division operations. When the exception occurs, the scale parameter refers to the fixed precision, and the subsequent numbers are rounded. * @paramV1 Dividend *@paramv2 Divisor *@paramthe scale representation needs to be accurate to several points after the decimal point. * @returntwo parameters of the quotient*/ Public Static DoubleDivDoubleV1,DoubleV2,intScale ) { if(scale<0){ Throw NewIllegalArgumentException ("The scale must is a positive integer or zero"); } BigDecimal B1=NewBigDecimal (double.tostring (v1)); BigDecimal B2=NewBigDecimal (Double.tostring (v2)); returnb1.divide (b2,scale,bigdecimal.round_half_up). Doublevalue (); } /*** Provides precise rounding of decimal digits. * @paramv need to round the number *@paramA few * after the decimal point@returnresults after rounding*/ Public Static DoubleRoundDoubleVintScale ) { if(scale<0){ Throw NewIllegalArgumentException ("The scale must is a positive integer or zero"); } BigDecimal b=NewBigDecimal (double.tostring (v)); BigDecimal One=NewBigDecimal ("1"); returnb.divide (one,scale,bigdecimal.round_half_up). Doublevalue (); } }
Basic operations for BigDecimal in "Java" Java