The example in this article tells you how to compare the PHP floating-point size. Share to everyone for your reference, specific as follows:
<?php
/**
* Floating-point numbers are generally not used to compare size, but we can use a flexible way to
use var_dump output floating-point is not see the effect, you can use Serialize view
* 1.round 2. Floating-point conversions to Strings
*
* Converting to String methods:
* Convert to String by preceding (string) or by using the Strval () function
* In an expression that requires a string, the string automatically transitions, such as using the function echo () or print (), or when a variable is compared to a string, the transformation
* True will change to 1, and false to an empty string
*
/$a = 13.2;
$b =;
$c = $a/$b;
The actual value is this d:0.54999999999999993338661852249060757458209991455078125;
Echo Serialize ($c). ' <br/> '//
echo $c. ' <br/> '//output will show 0.55 the actual value is smaller than his
//So direct and 0.55 comparison size is not tenable
if ($c = = 0.55) {
echo ' nothing ';
}
$c = Round ($c, 2);
Use round to process
if ($c = = 0.55) {
echo ' OK ';
}
echo "<br/>";
Force to a string
//$c = (string) $c;
$c = Strval ($c);
if ("$c" = = 0.55) {
echo ' OK ';
}
? >
The results of the operation are as follows:
d:0.54999999999999993338661852249060757458209991455078125;
0.55
OK
OK
For more information about PHP interested readers can view the site topics: "PHP Operations and Operator Usage Summary", "PHP basic Grammar Introductory Course", "PHP error and Exception handling method summary" and "PHP common functions and Skills summary"
I hope this article will help you with the PHP program design.