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