Sometimes "<" or ">" is used directly in PHP to compare the size of two floating point data types.
Method 1:
PHP provides a native method to solve this problem.
The following is a brief introduction to this native function :)
Bccomp
Descriptionint
Bccomp(String $ Left_operand, String $ Right_operand[, Int $ Scale])
ComparesLeft_operandToRight_operandAnd returns the result as an integer.
Parameters
-
Left_operand
-
The left operand, as a string.
-
Right_operand
-
The right operand, as a string.
-
Scale
-
The optionalScaleParameter is used to set the number of digits after the decimal place which will be used in the comparison.
-
Return values
Returns 0 if the two operands are equal, 1 ifLeft_operandIs larger thanRight_operand,-1 otherwise.
-
Example:
- Echo bccomp ('1', '2 '). "/N"; //-1 <br/> echo bccomp ('1. 00001 ', '1', 3); // 0 <br/> echo bccomp ('1. 00001 ', '1', 5); // 1 <br/>
-
Method 2:
Use the "-" operation directly. Compare the result of the difference between the two and 0.
-