PHP floating point size comparison method, php floating point size

Source: Internet
Author: User

PHP floating point size comparison method, php floating point size

This example describes how to compare the floating point size of PHP. We will share this with you for your reference. The details are as follows:

<? Php/*** floating point numbers are generally not used to compare the size, but we can use a flexible method * using var_dump to output floating point results does not show the effect, you can use serialize to view * 1. round 2. to convert a floating point to a string **: * to convert a string by adding a string before it or using the strval () function * in a string expression, the string is automatically converted, for example, when echo () or print () is used, or when a variable is compared with a string, this transformation * true is converted to 1, false is converted into an empty string **/$ a = 13.2; $ B = 24; $ c = $ a/$ B; // the actual value is d: 0.54999999999999993338661852249060757458209991455078125; echo serialize ($ c ). '<br/>'; // echo $ c. '< Br/> '; // The actual value displayed in the output is smaller than the actual value of 0.55. // Therefore, it is not true to directly compare the value with 0.55 if ($ c = 0.55) {echo 'nothing ';} $ c = round ($ c, 2); // process if ($ c = 0.55) {echo' OK ';} echo "<br/>"; // force convert to string // $ c = (string) $ c; // $ c = strval ($ c ); if ("$ c" = 0.55) {echo 'OK';}?>

The running result is as follows:

D: 0.54999999999999993338661852249060757458209991455078125; 0.55 okok

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.