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

PHP – ลบ Arbitrary Precision Numbers ด้วย bcsub()

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

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

string bcsub(string $num_str1, string $num_str2, int $scaleVal = 0)

พารามิเตอร์

  • $num_str1 – เลขถูกลบ (left operand) string
  • $num_str2 – เลขลบ (right operand) string
  • $scaleVal – จำนวนหลักทศนิยมในผลลัพธ์ (optional, default 0)

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

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

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

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

ผลลัพธ์

Output without scaleVal is: 7

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

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

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

ผลลัพธ์

Output with scaleVal is: 7.555