Comparison of floating point data in PHP

Source: Internet
Author: User

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.

 

 

 

 

 

 

 

 

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.