Because Java's simple types cannot accurately perform floating-point operations, this tool class provides precise floating-point operations, including addition, subtraction, multiplication, division, and rounding. It can be used directly in the past, which is very convenient.
Import java. Math. bigdecimal;/*** Because Java's simple type cannot accurately perform operations on floating point numbers, * this tool class provides precise floating point operations, including addition, subtraction, multiplication, division, and rounding. */Public class Arith {// default Division calculation precision: Private Static final int def_div_scale = 10; // This class cannot be instantiated private Arith () {}/*** provides precise addition operations. ** @ Param V1 * Add Number * @ Param V2 * Add Number * @ return and */public static double add (double V1, double V2) {bigdecimal b1 = new bigdecimal (V1); bigdecimal b2 = new bigdecimal (V2); Return b1.add (B2 ). doublevalue ();}/*** provides precise subtraction. ** @ Param V1 * subtrahend * @ Param V2 * subtrahend * @ Return Difference Between 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 exact multiplication. ** @ Param V1 * multiplier * @ Param V2 * multiplier * @ return 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 (relatively) Precise Division operations. In case of division, * is accurate to 10 digits after the decimal point, and the digits after the decimal point are rounded down. ** @ Param V1 * divisor * @ Param V2 * divisor * @ return two parameters */public static double Div (double V1, double V2) {return Div (V1, v2, def_div_scale);}/*** provides (relatively) accurate Division operations. In case of division, the scale parameter determines the precision. * The number after division is rounded down. ** @ Param V1 * divisor * @ Param V2 * divisor * @ Param scale * indicates the number of digits after the decimal point. * @ Return operator of two parameters */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 precise rounded decimal places. ** @ Param v * number to be rounded off * @ Param scale * number of digits to be retained after the decimal point * @ return result after rounding */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 ();}};