Chapter 2 Precise Calculation of Java floating point numbers and Chapter 2 java floating point numbers

Source: Internet
Author: User

Chapter 2 Precise Calculation of Java floating point numbers and Chapter 2 java floating point numbers

1. Practical Significance

In actual development, if precise float or double computing (especially financial computing) is required ), directly Using float or double is not acceptable (for specific examples, refer to the test results of the main method of the code below). BigDecimal is required.

 

2. Code

Package com. xxx. util; import java. math. bigDecimal;/*** floating point number precision algorithm */public class BigDecimalArithUtil {private static final int DIV_SCALE = 10; // division precision (10 decimal points are reserved except for all values) /** exact addition of decimals */public static double add (double d1, double d2) {BigDecimal bd1 = BigDecimal. valueOf (d1); BigDecimal bd2 = BigDecimal. valueOf (d2); return bd1.add (bd2 ). doubleValue ();}/** exact decimal subtraction */public static double sub (double d1, double d2) {BigDecimal bd1 = BigDecimal. valueOf (d1); BigDecimal bd2 = BigDecimal. valueOf (d2); return bd1.subtract (bd2 ). doubleValue ();}/** exact fractional multiplication */public static double mul (double d1, double d2) {BigDecimal bd1 = BigDecimal. valueOf (d1); BigDecimal bd2 = BigDecimal. valueOf (d2); return bd1.multiply (bd2 ). doubleValue ();}/** precise fractional Division */public static double div (double d1, double d2) {BigDecimal bd1 = BigDecimal. valueOf (d1); BigDecimal bd2 = BigDecimal. valueOf (d2);/** when division is not complete, it is rounded down (there are many processing methods for division of values) retain 10 decimal places */return bd1.divide (bd2, DIV_SCALE, BigDecimal. ROUND_HALF_UP ). doubleValue ();} public static void main (String [] args) {// test addition System. out. println ("0.05 + 0.01 =" + BigDecimalArithUtil. add (0.05, 0.01); System. out. println ("0.05 + 0.01 =" + (0.05 + 0.01); // test the subtraction System. out. println ("1.0-0.42 =" + BigDecimalArithUtil. sub (1.0, 0.42); System. out. println ("1.0-0.42 =" + (1.0-0.42); // test the multiplication System. out. println ("4.015*100 =" + BigDecimalArithUtil. mul (4.015, 100); System. out. println ("4.015*100 =" + (4.015*100); // test division System. out. println ("123.3/100 =" + BigDecimalArithUtil. div (123.3, 100); System. out. println ("123.3/100 =" + (123.3/100 ));}}View Code

 

3. Notes

  • The above program I used is BigDecimal. valueOf (double x) is used to encapsulate double Type x into BigDecimal. view the source code as follows:/*** note: this is usually the best way to convert double and float to a BigDecimal */public static BigDecimal valueOf (double val) {return new BigDecimal (Double. toString (val ));}View Code

    Here, double is directly converted to String, and then converted to BigDecimal using the following constructor.

     public BigDecimal(String val)

    This is the best practice. If the double or float is directly converted to BigDecimal, that is, the following constructor is used, the exact result may not be obtained. (See comments)

    /*** Note: The results of this constructor can be somewhat unpredictable. * The constructor result is sometimes inaccurate */public BigDecimal (double val)View Code
  • In actual use, int and long may also be used for exact calculation of Floating Point Numbers (the specific method is to multiply the floating point number by the corresponding multiples to int (<= 9 decimal number) or long (<= 18-digit decimal number), which is calculated and divided by the previous multiple to obtain the result), and the performance of this method is higher, for the specific use of int, long, And BigDecimal, see article 48th objective Java (version 2.
  • It should be pointed out that, in actual development, the performance of BigDecimal is basically negligible, and is the first choice for exact floating point calculation. According to the previous article, if the integer converted from a floating point number is greater than 18 digits, BigDecimal must also be used.

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.