Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP – คูณ Arbitrary Precision Numbers ด้วย bcmul()

ฟังก์ชัน bcmul() ใช้คูณ arbitrary precision number หนึ่งกับอีกตัวหนึ่ง รับค่า 2 ตัวเป็น string คืนค่าผลคูณหลังปัดเศษตาม precision

รูปแบบการใช้งาน (Syntax)

string bcmul(string $num_string1, string $num_string2, int $scaleVal = 0)

พารามิเตอร์

  • $num_string1 – เลขตัวแรก (left operand) string
  • $num_string2 – เลขตัวที่สอง (right operand) string
  • $scaleVal – จำนวนหลักทศนิยมในผลลัพธ์ (optional, default 0)

ค่าที่คืนกลับ

คืนผลคูณเป็น string

ตัวอย่าง 1: ไม่ใช้ scaleVal

<?php
$num_string1 = "10.5552";
$num_string2 = "3";
$result = bcmul($num_string1, $num_string2);
echo "Output without scaleVal is: " . $result;
?>

ผลลัพธ์

Output without scaleVal is: 31

ไม่ใช้ scaleVal จะตัดทศนิยมทิ้ง

ตัวอย่าง 2: ใช้ scaleVal = 4

<?php
$num_string1 = "10.5552";
$num_string2 = "3";
$scaleVal = 4;
$result = bcmul($num_string1, $num_string2, $scaleVal);
echo "Output with scaleVal is: " . $result;
?>

ผลลัพธ์

Output with scaleVal is: 31.6656