PHP
 Computer >> コンピューター >  >> プログラミング >> PHP

PHP – bcadd()関数を使用して2つの任意精度の数値を追加するにはどうすればよいですか?


PHPでは、 bcadd() 数学関数は、2つの任意精度の数値を加算するために使用されます。 bcadd() 関数は2つのランダムな精度の数値を文字列として受け取り、結果を識別された精度にスケーリングした後、2つの数値の加算を返します。

構文

string bcadd ( $num_str1, $num_str2, $scaleVal)

パラメータ

bcadd() 数学関数は、 $ num_str1、$ num_str2の3つの異なるパラメーターを受け入れます。 および$scaleVal。

  • $ num_str1- 左のオペランドを表し、文字列型のパラメータです。

  • $ num_str2- これは右のオペランドを表し、文字列タイプのパラメータです。

  • $ scaleVal- これは、結果の出力の小数点以下の桁数を設定するために使用されるオプションのパラメーターです。デフォルトでは0を返します。

戻り値

bcadd() 数学関数は、2つのオペランドの合計を返します $ num_str1 およびnum_str2 、文字列として。

例1-$scaleValパラメーターを使用しないbcadd()PHP関数
<?php
   // PHP program to illustrate bcadd() function
   // two input numbers using arbitrary precision
   $num_string1 = "5";
   $num_string2 = "10.555";

   // calculates the addition of
   // the two numbers without $scaleVal
   $result = bcadd($num_string1, $num_string2);
   echo "Output without scaleVal is: ", $result;
?>

出力

Output without scaleVal is: 15

説明- 上記のPHPの例では、2つのパラメーターのみ $ num_string1 および$num_string2 bcadd()を使用して2つの数値の加算を計算するために使用されます 働き。 $ scaleval パラメータは使用されず、出力値は15になり、15以降の精度の数値は削除されます。

例2-$scaleValパラメーターを使用したbcadd()PHP関数

ここで、同じ入力値を $ scaleValと一緒に使用してみましょう。 パラメータを設定し、出力を確認します。

<?php
   // PHP program to illustrate bcadd() function
   // two input numbers using arbitrary precision
   $num_string1 = "5";
   $num_string2 = "10.555";

   //using scale value 2
   $scaleVal = 2;

   // calculates the addition of
   // two numbers with $scaleVal parameter
   $result = bcadd($num_string1, $num_string2, $scaleVal);
   echo "Output with scaleVal is: ", $result;
?>

出力

Output with scaleVal is: 15.55

  1. PHPでimageellipse()関数を使用して楕円を描く方法は?

    imageellipse() 楕円を描くために使用されるPHPの組み込み関数です。成功するとTrueを返し、失敗するとFalseを返します。 構文 Bool imageellipse($image, $cx, $cy, $width, $height, $color) パラメータ imageellipse() 6つの異なるパラメータを取ります: $ image 、 $ cx 、 $ cy 、 $ width 、 $ height 、 $ color 。 $ image −画像のサイズを作成します。これは、imagecreatetruecolor()などの画像作成関数の

  2. PHPでimagecropauto()関数を使用して画像を自動的にトリミングするにはどうすればよいですか?

    imagecropauto() はPHPに組み込まれている関数で、使用可能なモードの1つを使用して画像を自動的にトリミングするために使用されます。 構文 resource imagecropauto(resource $image, int $mode, float $threshold, int $color) パラメータ imagecropauto() 4つの異なるパラメータを取ります-$image、$ mode、$ threshold および$color 。 $ image −トリミングする画像リソースを指定します。 $ mode −これはオプションのパラメー