Deep analysis of the problem of PHP handling floating-point numbers

Source: Internet
Author: User
Tags sprintf

below to introduce you to the problem of PHP processing floating-point numbers, if you encounter these problems in the process of use we can come together to see, I hope the article will help you

Companies to adjust the price of products, because do the foreign trade Mall, so the price should be in line with international standards. For example, foreign prices show the way is: the mark for $35.00; The $56.2 is identified as: $56.20.

The above requirements were addressed through sprintf ("%1\$.2f", $price), but new problems arose, with a price of 0 being processed at 0.00.

Through empty () and determine whether the equality, unrecognized number of points 0.00; Through Baidu summed up the following methods of processing floating-point numbers.

Handling of floating-point 0.00

The conversion from intval to Plastic intval (0.00) to 0, only for 0.00 use intval processing; you can see the first example.

Example 1:

$n = "19.99″;

Print intval ($n *100); The structure of the output is 1998, not 1999;

Print Intval (Strval ($n *100));//This output is 1999;

Example two:

Echo Floor ((0.1+0.7) *10);//output is 7, not 8;

Echo Floor (Strval (0.1+0.7) *10);//this is 8;

In PHP, some simple floating-point data cannot be represented internally by an exact binary. This is related to the data representation of the computer: it is not possible to represent certain decimal fractions in a finite binary. Never believe that the result of a floating-point number is accurate to the last, and never compare two floating-point numbers for equality.

The 2 examples above summarize how PHP handles floating-point numbers by turning them into strings, either by Strval or by using printf/sprintf to convert floating-point numbers into strings.

Floating point Accuracy

It is obvious that a simple decimal score like 0.1 or 0.7 cannot be converted to an internal binary format without losing a little bit of precision. This can result in confusion: for example, floor ((0.1+0.7) *10 typically returns 7 instead of 8 as expected because the internal representation of the result is actually similar to 7.9999999999 ....

This is related to the fact that it is impossible to accurately express certain decimal fractions with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333. . .。

Deep analysis of the problem of PHP handling floating-point numbers

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.