$ B = (0.1 + 0.7) * 10; echointval ($ B); why output 0.7?

Source: Internet
Author: User
{Code...} Why is 0.7 output? Not 0.8?
php




$b=(0.1+0.7)*10;echo intval($b);

Why is the output 0.7? Not 0.8?

Reply content:
php




$b=(0.1+0.7)*10;echo intval($b);

Why is the output 0.7? Not 0.8?

It is related to the nature of floating-point calculation. If you directly output $ B, you will find that the calculation result is 0.7999999, while intval is rounded down. You can use ceil instead.
See http://php.net/manual/en/language.types.integer.php#language.types.integer.casting

Let's take a look.$bBinary format:


  

Output:

ffffffffffff1f40

Becauselittle-endianIs actually0x401fffffffffffffAccordingIEEE754If the standard conversion is decimal

7.999999999999999

phpYou can useJavaTry the following code:

System.out.println(Double.longBitsToDouble(0x401fffffffffffffL));

The other content is clearly explained by the number of people upstairs ~

Http://www.laruence.com/2013/03/26/2884.html

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.