Bigdecimal implements Precise Calculation of Java floating point numbers

Source: Internet
Author: User

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/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.