createtime--2017 December 1 11:35:00
Author:marydon
Arithmetic tool classes between Java floating-point types (float, double)
/*** Tool class for subtraction, rounding and other operations of BigDecimal objects *@authorMarydon * @createTime December 1, 2017 morning 11:39:15 * @updateTime * @Email: [Email protected] * @description because Java's simple type cannot accurately Floating-point numbers, the tool class provides accurate floating-point arithmetic, including subtraction and rounding. * @version: 1.0.0*/ Public classArithmeticutiles {/*** Tool class for subtraction, rounding and other operations of BigDecimal objects *@authorWupenghui The tool class provides precise floating-point arithmetic, including subtraction and rounding, because Java's simple type does not accurately perform operations on floating-point numbers. */ //Default division operation Precision Private Static Final intDef_div_scale = 10; //This class cannot be instantiated Privatearithmeticutiles () {}/*** provides precise 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 precise subtraction * *@paramv1 * minuend *@paramv2 * meiosis *@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 circumstances, accurate to 10 digits after the decimal point, after the number rounding *@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 an exception occurs, the precision is specified by the scale parameter, and the subsequent number is rounded. * * @paramV1 * Dividend *@paramv2 * Divisor *@paramScale * Indicates the need 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 * Numbers that need to be rounded *@paramScale * Keep several after 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 (); } /*** provides exact type conversion (Float) * *@paramv * Numbers that need to be converted *@returnreturns the result of the conversion*/ Public Static floatConvertstofloat (Doublev) {BigDecimal B=NewBigDecimal (v); returnB.floatvalue (); } /*** Provide exact type conversions (INT) without rounding * *@paramv * Numbers that need to be converted *@returnreturns the result of the conversion*/ Public Static intConvertstoint (Doublev) {BigDecimal B=NewBigDecimal (v); returnB.intvalue (); } /*** provides exact type conversion (Long) * *@paramv * Numbers that need to be converted *@returnreturns the result of the conversion*/ Public Static LongConvertstolong (Doublev) {BigDecimal B=NewBigDecimal (v); returnB.longvalue (); } /*** Returns the large value of two numbers * *@paramV1 * The first number to be compared *@paramV2 * A second number to be compared *@returnreturns a value that is large in two numbers*/ Public Static DoubleReturnmax (DoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (v1); BigDecimal B2=NewBigDecimal (v2); returnB1.max (B2). Doublevalue (); } /*** Returns a value of two decimal digits * *@paramV1 * The first number to be compared *@paramV2 * A second number to be compared *@returnreturns a value in the middle of a two number*/ Public Static DoubleReturnmin (DoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (v1); BigDecimal B2=NewBigDecimal (v2); returnb1.min (B2). Doublevalue (); } /*** Accurate comparison of two numbers * *@paramV1 * The first number to be compared *@paramV2 * A second number to be compared * *@returnReturns 0 if the two number is the same, or 1 if the first number is larger than the second, and returns 1*/ Public Static intCompareTo (DoubleV1,Doublev2) {BigDecimal B1=NewBigDecimal (v1); BigDecimal B2=NewBigDecimal (v2); returnB1.compareto (B2); }}
Java floating point arithmetic