Recommendation 22: Processing currencies with integer types

Source: Internet
Author: User

In daily life, the most easy to access the decimal is the currency, such as you pay the salesperson 10 yuan to buy a 9.6-dollar snack, the salesperson should find you 0.4 yuan is 4 cents is right, we look at the following procedure:

public class Client {public static void main (string[] args) {System.out.println (10.00-9.60);}}
We expect the result to be 0.4, it should be the number, but the print out is 0.40000000000000036, this is why?

This is because floating-point numbers may be inaccurate in a computer (note that it is possible), and it can only be infinitely close to the exact value, not completely accurate. Why is it so? This is determined by the storage rules with floating-point numbers, let's first look at 0.4 how this decimal decimal number is converted to binary, using the "Multiply 2 rounding, order" method (do not understand?) This is not a recruit, too basic, we found that 0.4 can not use the binary precision representation, in the binary world he is an infinite loop of decimals, that is, "display" Can not "show", not to mention in memory (floating point storage includes three parts: sign bits, digits, mantissa, specific no longer described), It can be understood that in the decimal world there is no way to accurately represent 1/3, that in the binary world of course it is not possible to accurately represent 1/5 (if the binary is also a fraction of the words can be expressed), in the binary world 1/5 is an infinite loop decimal.

You guys are going to say it, so I'm not going to get the whole result? The code is as follows:

public class Client {public static void main (string[] args) {NumberFormat nf = new DecimalFormat ("#.##"); System.out.println (Nf.format (10.00-9.60));}}
Printing the result is 0.4, seemingly solved, but hides a very deep problem. Let's think about how the financial industry calculates, accounting systems generally record four decimal places after the decimal point but in the summary, presentation, and report, only two decimal places after the decimal point are recorded, if you use floating-point numbers to calculate the currency, think about it, How much of a gap will result after a large number of subtraction (which also involves rounding issues that will be mentioned later)! The accounting system is accurate, but because of the computer's sake is not accurate, it is a sin, there are two ways to solve this problem:

(1) Using BigDecimal

BigDecimal is a class designed specifically to compensate for the lack of accurate calculation of floating-point numbers, and it also provides a common mathematical algorithm for subtraction. BigDecimal is the optimal solution, especially when it is mapped to a field with a database decimal type.

(2) using integral type

By enlarging the value of the participating operation by 100 times times and converting it to an integral type, and then reducing it by 100 times times in the presentation, the benefits of this processing are simple and accurate, and generally more widely used in non-financial industries such as retail. This method is also used for some retail POS machines, and their inputs and outputs are all integers, and the operation is simpler.

==========================================

I follow the author's statement, the floating-point subtraction operation I use the BigDecimal type, but can not see what is the difference between a double? As follows:


Recommendation 22: Handling currencies with integer types

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.