Home > Backend Development > PHP Tutorial > PHP - How to compare two arbitrary precision numbers using bccomp() function?

PHP - How to compare two arbitrary precision numbers using bccomp() function?

WBOY
Release: 2023-08-20 08:46:01
forward
2352 people have browsed it

PHP - 如何使用bccomp()函数比较两个任意精度的数字?

In PHP, the bccomp() function is used to compare two arbitrary numbers. bccomp()The function accepts two arbitrary-precision numeric strings as input and outputs an integer after comparing the two numbers.

Syntax

int bccomp($left_string1, $right_string1, $scaleval)
Copy after login

Parameters

Functionbccomp()Accepts three different parameters− $left_string1, $right_string2 and $scaleval.

  • $left_string1− represents the left operand of one of the two given numbers we want to compare, which is a parameter of type string.

  • $right_string2− represents the right operand of one of the two given numbers we want to compare, which is a parameter of type string.

  • $scaleval−Returns the number of decimal places that will be used in the comparison. It is an argument of type integer. The default value is zero.

Return value

Functionbccomp() Returns two numbers $left_string1 and $right_string2 comparison results.

  • If $left_string1 is greater than $right_string2, return 1.

  • If $left_string1 is less than $right_string2, return -1.

  • If the two given numbers are equal, the function bccomp() returns 0.

Example 1− bccomp() PHP function using equal parameters

<?php
   // input two numbers
   $left_string1 = "3.12";
   $right_string2 = "3";

   // calculates the comparison of the two
   //number without scale value
   $result = bccomp($left_string1, $right_string2);

   //used equal parameters
   echo "The result is: ", $result;
?>
Copy after login

Output

The result is: 0
Copy after login

The above program returns 0 because there is no proportional value in case where equal parameters are used.

Example 2

<?php
   // input two numbers
   $left_string1 = "30.12"; // left value > right value
   $right_string2 = "3";

   //used scale value two
   $scaleval = 2;

   // calculates the comparison of the two
   //number without scale value
   $result = bccomp($left_string1, $right_string2);

   //used equal parameters
   echo "The output is: ", $result;
?>
Copy after login

Output

The output is: 1
Copy after login

It returns 1 because the lvalue is greater than the rvalue.

Example 3

<?php
   // input two numbers
   $left_string1 = "30.12";
   $right_string2 = "35"; // Right value > Left value

   //used scale value two
   $scaleval = 2;

   // calculates the comparison of the two
   //number without scale value
   $result = bccomp($left_string1, $right_string2);

   //used equal parameters
   echo $result;
?>
Copy after login

Output

-1
Copy after login

It returns -1 because the Right value is greater than the Left value.

The above is the detailed content of PHP - How to compare two arbitrary precision numbers using bccomp() function?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template