Import Java. math. bigdecimal; <br/>/** <br/> * Because Java's simple type cannot accurately perform operations on floating point numbers, this tool provides precision <br/> * floating point number operations, including addition, subtraction, multiplication, division, and rounding. <Br/> */<br/> public class Arith {<br/> // default Division calculation precision <br/> Private Static final int def_div_scale = 10; <br/> // This class cannot be instantiated <br/> private Arith () {<br/>}</P> <p>/** <br/> * provides precise addition operations. <Br/> * @ Param V1 add count <br/> * @ Param V2 add count <br/> * @ return and <br/> */<br/> Public static double add (double V1, double V2) {<br/> bigdecimal b1 = new bigdecimal (double. tostring (V1); <br/> bigdecimal b2 = new bigdecimal (double. tostring (V2); <br/> return b1.add (B2 ). doublevalue (); <br/>}< br/>/** <br/> * provides precise subtraction. <Br/> * @ Param V1 subtrahend <br/> * @ Param V2 <br/> * @ return <br/> */<br/> Public static double sub (double V1, double V2) {<br/> bigdecimal b1 = new bigdecimal (double. tostring (V1); <br/> bigdecimal b2 = new bigdecimal (double. tostring (V2); <br/> return b1.subtract (B2 ). doublevalue (); <br/>}< br/>/** <br/> * provides exact multiplication. <Br/> * @ Param V1 multiplier <br/> * @ Param V2 multiplier <br/> * @ return product of two parameters <br/> */<br/> Public static double MUL (double V1, double V2) {<br/> bigdecimal b1 = new bigdecimal (double. tostring (V1); <br/> bigdecimal b2 = new bigdecimal (double. tostring (V2); <br/> return b1.multiply (B2 ). doublevalue (); <br/>}</P> <p>/** <br/> * provides (relatively) Precise Division operations, in case of division, it is accurate to <br/> * 10 digits after the decimal point, and the digits after the decimal point are rounded down. <Br/> * @ Param V1 divisor <br/> * @ Param V2 divisor <br/> * @ return two parameter vendors <br/> */<br/> Public static double Div (double V1, double V2) {<br/> return Div (V1, V2, def_div_scale ); <br/>}</P> <p>/** <br/> * provides (relatively) accurate Division operations. In case of division, the scale parameter indicates <br/> * precision, and the number is rounded down. <Br/> * @ Param V1 divisor <br/> * @ Param V2 divisor <br/> * @ Param scale indicates the number of digits after the decimal point. <Br/> * @ return operator of two parameters <br/> */<br/> Public static double Div (double V1, double V2, int scale) {<br/> If (scale <0) {<br/> throw new illegalargumentexception (<br/> "the scale must be a positive integer or zero "); <br/>}< br/> bigdecimal b1 = new bigdecimal (double. tostring (V1); <br/> bigdecimal b2 = new bigdecimal (double. tostring (V2); <br/> return b1.divide (B2, scale, bigdecimal. round_half_up ). doubl Evalue (); <br/>}</P> <p>/** <br/> * provides precise rounding of decimal places. <Br/> * @ Param V: number to be rounded up <br/> * @ Param scale: number of digits to be retained after the decimal point <br/> * @ return result after rounding <br/> */<br/> Public static double round (Double V, int scale) {<br/> If (scale <0) {<br/> throw new illegalargumentexception (<br/> "the scale must be a positive integer or zero "); <br/>}< br/> bigdecimal B = new bigdecimal (double. tostring (v); <br/> bigdecimal one = new bigdecimal ("1"); <br/> return B. divide (one, scale, bigdecimal. round_half_up ). doublevalue (); <br/>}< br/>}