Java exact operation (BigDecimal) __java

Source: Internet
Author: User

(1), floating point precision calculation    Shengli oilfield three-flow integration project has always been a problem, that is, each report of the amount of material and the actual amount of money to be poor, and the actual amount is inconsistent, so that customers feel always less comfortable, The reason is because we use Java floating-point type double to define the material amount, and in the report statistics we often have to do some operations, but the Java floating-point number (double, float) calculation is accurate calculation, see the following example:       system.out.println (0.05 + 0.01);        System.out.println (1.0 - 0.42);       system.out.println (4.015 *  100);       system.out.println (123.3 / 100);   what your expected output is. The actual output is indeed such:       0.060000000000000005   0.5800000000000001   401.49999999999994   1.2329999999999999   This problem is very serious, If you have 123.3 yuan to buy goods, but the computer thinks you only 123.29999999999999 yuan, not enough money, the computer refused to trade.    (2), rounded    whether it can be rounded. Of course, it's customary for us to think like this, but rounding means errors, business operations can mean errors, and Java does not provide a rounding method that preserves a specified number of digits, providing only one math.round (double d) and Math.Round (FLOAT F), which return long integer and integer values, respectively. Round method cannot be set to hold a few decimal places, we can only do this (keep two-bit):  Public double round (Double value) {   Return math.round ( value *  100 )  / 100.0;  }   But unfortunately, the code above does not work properly, Pass 4.015 to this method and it will return 4.01 instead of 4.02, as we saw above in the    4.015 * 100 = 401.49999999999994    So if we want to do a precise rounding, this method does not meet our requirements.    there is another way to use Java.text.DecimalFormat, but there are also problems with the rounding mode used in format is Round_half_down (the rounding mode is described below), For example, 4.025 preserves two decimal places is 4.02, because. 025 is equal to the length of " nearest neighbor" (. 02 and. 03), and rounding down is. 02, if it is 4.0251, then keeping the two decimal number is 4.03.    System.out.println (New java.text.decimalformat ("0.00"). Format (4.025));   System.out.println (New java.text.decimalformat ("0.00"). Format (4.0251));   output is    4.02    4.03       (3), floating-point output (scientific notation)    Java floating-point values are automatically converted to scientific notation when they are greater than 9999999.0, and we look at the following example:       system.out.println ( 999999999.04);       system.out.println (99999999.04);       system.out.println (10000000.01);        The results of System.out.println (9999999.04);   output are as follows:       9.9999999904E8    9.999999904e7   1.000000001e7   9999999.04        But sometimes we may not need scientific notation method, need to convert to string, not directly with ToString () and other methods of conversion, very cumbersome.    BigDecimal Introduction       BigDecimal is a constant, arbitrary-precision, signed decimal-number object provided by Java. It provides four constructors, two of which are constructed with BigInteger, where we don't care, we focus on the two constructors that are constructed with double and string (refer to the J2SE API documentation for BigInteger details).    BigDecimal (double val)               Translates a double into a BigDecimal.   BigDecimal (String val)   

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.