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

WBOY
Libérer: 2023-08-20 08:46:01
avant
2155 人浏览过

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

在PHP中,bccomp()函数用于比较两个任意数字。 bccomp()函数接受两个任意精度的数字字符串作为输入,并在比较这两个数字后输出一个整数。

Syntax

int bccomp($left_string1, $right_string1, $scaleval)
Copier après la connexion

参数

函数bccomp()接受三个不同的参数− $left_string1$right_string2$scaleval

  • $left_string1−表示我们要进行比较的两个给定数字之一的左操作数,它是一个字符串类型的参数。

  • $right_string2−表示我们要进行比较的两个给定数字之一的右操作数,它是一个字符串类型的参数。

  • $scaleval−返回在比较中将使用的小数位数,它是一个整数类型的参数,默认值为零。

返回值

函数bccomp() 返回两个数字$left_string1$right_string2的比较结果。

  • 如果$left_string1大于$right_string2,则返回1

  • 如果$left_string1小于$right_string2,则返回-1

  • 如果两个给定的数字相等,则函数bccomp()返回0

示例1− 使用相等参数的bccomp() PHP函数

Copier après la connexion

输出

The result is: 0
Copier après la connexion

上述程序返回0,因为在没有比例值的情况下使用了相等的参数。

示例2

 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;
?>
Copier après la connexion

输出

The output is: 1
Copier après la connexion

它返回1,因为左值大于右值。

示例3

 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;
?>
Copier après la connexion

输出

-1
Copier après la connexion

它返回-1,因为Right值大于Left值。

以上是PHP - 如何使用bccomp()函数比较两个任意精度的数字?的详细内容。更多信息请关注PHP中文网其他相关文章!

source:tutorialspoint.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!