PHP Floor Value Issues

Source: Internet
Author: User

This is the case, in the company has a customer response will be one less into a number, probably similar to Baidu Encyclopedia

<?php$str=41.23*5000;echo Floor ($STR), echo "<br>", Echo intval ($STR);? > should get 206150, why is he getting 206149?

Workaround:

You can use Strval to turn around.

Reason:

This is an internal problem. The calculation of floating-point numbers is never completely accurate, for example, 1/3 means 0.333333 ... after n 3, but the calculation always omits the subsequent 3, which causes these phenomena such as echo "<br>". Intval ( (0.1+0.7) *10)//0.7echo "<br>". Intval (Strval (0.1+0.7) *10);//0.8 I am 32 for the system, the result is this, but 64 is not clear, the floating-point number in different systems, the accuracy is not the same, The results may not be the same. You can use Strval to turn your operation looks like 206150, in fact, the internal computer is 206149.99999 ... In the form of a floor operation, and that's what happens when you use the floor.


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.