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

PHP – หาร Arbitrary Precision Numbers ด้วย bcdiv()

ฟังก์ชัน bcdiv() ใช้หาร arbitrary precision number หนึ่งด้วยอีกตัวหนึ่ง (dividend ÷ divisor) รับค่า 2 ตัวเป็น string คืนค่าผลลัพธ์หลังปัดเศษตาม precision

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

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

พารามิเตอร์

  • $num_string1 – dividend (ตัวถูกหาร) string
  • $num_string2 – divisor (ตัวหาร) string
  • $scaleVal – จำนวนหลักทศนิยมในผลลัพธ์ (optional, default 0)

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

คืนผลลัพธ์การหารเป็น string

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

<?php
$num_string1 = "22.5552";
$num_string2 = "5";
$result = bcdiv($num_string1, $num_string2);
echo "Output without using Scale Value: " . $result;
?>

ผลลัพธ์

Output without using Scale Value: 4

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

<?php
$num_string1 = "22.5552";
$num_string2 = "5";
$scaleVal = 4;
$result = bcdiv($num_string1, $num_string2, $scaleVal);
echo "Output with Scale Value is: " . $result;
?>

ผลลัพธ์

Output with Scale Value is: 4.5110