Precise business computing in Java

Source: Internet
Author: User
Tags mul

Due to the rounding relationship of the computer in the computing process, normal computing and engineering computing may be inaccurate enough, which may be tolerable in the two calculation methods, but it is unacceptable in business operations, financial computing.

// Import Org. zym. arithutil. arith; </P> <p> public class floatcalculation {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> system. out. println (0.05 + 0.01); <br/> // system. out. println (Arith. add (0.05, 0.01); </P> <p> system. out. println (4.015*100); <br/> // system. out. println (Arith. mul (4.015, 100); <br/>}</P> <p >}< br/>

The calculation result is as follows:

0.060000000000000005
401.49999999999994

This is obviously unacceptable.

 

The following provides an arithmetic tool to solve this problem. The basic principle is to use the java. Math. bigdecimal class to perform addition, subtraction, multiplication, division, and encapsulation.

 

Package Org. zym. arithutil; <br/> Import Java. math. bigdecimal; </P> <p> 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/>

 

See the following code:

 

Import Org. zym. arithutil. arith; </P> <p> public class floatcalculation {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> system. out. println (0.05 + 0.01); <br/> system. out. println (Arith. add (0.05, 0.01); </P> <p> system. out. println (4.015*100); <br/> system. out. println (Arith. mul (4.015, 100); </P> <p >}< br/>

 

The comparison results are as follows: Write 0.060000000000000005
0.06
401.49999999999994
401.5

With satisfactory results, you can test the other two types of computation on your own. This solves some issues that require precise computation of business users.

Related Article

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.