Computer >> 컴퓨터 >  >> 프로그램 작성 >> PHP

PHP – bcmul() 함수를 사용하여 두 개의 임의 정밀도 숫자를 곱하는 방법은 무엇입니까?


PHP에서 bcmul() 수학 함수는 임의의 정밀도 숫자를 다른 숫자와 곱하는 데 사용됩니다. bcmul() 함수는 두 개의 임의의 정밀도 숫자를 문자열로 취하고 결과를 식별된 정밀도로 스케일링한 후 두 숫자의 곱으로 결과를 제공합니다.

구문

string bcmul( $num_string1, $num_string2, $scaleVal)

매개변수

bcmul() 수학 함수는 세 가지 매개변수를 허용합니다. $num_string1, $num_string2 $scaleVal.

  • $num_string1 - 왼쪽 피연산자를 나타내며 문자열 유형 매개변수입니다.

  • $num_string2 - 오른쪽 피연산자를 나타내며 문자열 유형 매개변수입니다.

  • $scaleVal - 이것은 결과 출력에서 ​​소수점 이하 자릿수를 설정하는 데 사용되는 선택적 정수 유형 매개변수입니다. 기본값 0을 반환합니다.

반환 값

bcmul() 수학 함수는 두 숫자의 곱을 반환합니다. $num_str1 num_str2 , 문자열로.

예제 1 - $scaleVal 매개변수를 사용하지 않는 bcmul() PHP 함수

<?php
   // PHP program to illustrate bcmul() function
   // two input numbers using arbitrary precision
   $num_string1 = "10.5552";
   $num_string2 = "3";

   // calculates the addition of
   // two numbers without $scaleVal parameter
   $result = bcmul($num_string1, $num_string2);

   echo "Output without scaleVal is: ", $result;
?>

출력

Output without scaleVal is: 31

scaleVal 없이 매개변수, 소수점 이하 자릿수는 버립니다.

예제 2 - $scaleVal() 매개변수를 사용하는 bcmul() PHP 함수

여기에서는 scale 값이 4인 동일한 입력 값을 사용하고 출력을 확인합니다.

<?php
   // PHP program to illustrate bcmul() function
   // two input numbers using arbitrary precision
   $num_string1 = "10.5552";
   $num_string2 = "3";

   // using scale value 4
   $scaleVal = 4;

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

출력

Output with scaleVal is: 31.6656