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

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

WBOY
WBOYforward
2023-08-20 08:45:412341browse

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)

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;
?>

Output

The result is: 0

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;
?>

Output

The output is: 1

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;
?>

Output

-1

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete