最初发表于Takeshi Yu的博客。
准确的数值计算在企业应用程序中至关重要,尤其是那些涉及财务、会计或库存的应用程序。 即使很小的舍入误差也会导致严重的问题。 PHP 8.4 增强的 BCMath 对象 API 为精确高效的小数计算提供了完善的解决方案。
经验丰富的 PHP 开发人员熟悉浮点不精确性:
<code class="language-php">$a = 0.1; $b = 0.2; var_dump($a + $b); // Outputs: 0.30000000000000004</code>
这种不准确在金融环境中是不可接受的。 这些小错误累积起来,就会导致现实世界的差异。
精确的小数计算从数据库开始。 DECIMAL
类型是必需的:
<code class="language-php">// In Laravel Migration Schema::create('items', function (Blueprint $table) { $table->id(); $table->decimal('quantity', 10, 3); // Precision: 10 digits, 3 decimal places $table->decimal('price', 10, 3); // Precision: 10 digits, 3 decimal places $table->decimal('discount', 10, 3); // Precision: 10 digits, 3 decimal places $table->decimal('tax', 10, 3); // Precision: 10 digits, 3 decimal places // ... other columns });</code>
DECIMAL
确保:
虽然可能比 FLOAT
稍慢,但精度优势超过了关键任务系统中的性能差异。
Laravel 通过其转换系统简化了小数处理:
<code class="language-php">class Item extends Model { protected $casts = [ 'quantity' => 'decimal:3', 'price' => 'decimal:3', 'discount' => 'decimal:3', 'tax' => 'decimal:3', ]; }</code>
但是,请记住 Laravel 铸造主要管理:
即使使用正确的数据库类型和 Laravel 转换,也可能会出现计算错误:
<code class="language-php">// Database values $item1 = Item::find(1); // price: "99.99" $item2 = Item::find(2); // price: "149.99" // Calculation without BCMath $subtotal = $item1->price + $item2->price; $tax = $subtotal * 0.05; // 5% tax var_dump($tax); // Outputs: float(12.499000000000002) instead of 12.499</code>
发生这种情况是因为 PHP 在算术过程中隐式将字符串转换为数字:
<code class="language-php">// String values from database $price1 = "99.99"; $price2 = "149.99"; echo gettype($price1); // string // Implicit conversion to float $total = $price1 + $price2; echo gettype($total); // double (float)</code>
传统的 BCMath 扩展提供了精度:
<code class="language-php">// Database values $item1 = Item::find(1); // price: "99.99" $item2 = Item::find(2); // price: "149.99" // Using BCMath functions $subtotal = bcadd($item1->price, $item2->price, 3); $tax = bcmul($subtotal, $item2->tax, 3); var_dump($tax); // Precisely outputs: string(5) "12.499"</code>
然而,复杂的计算变得冗长且难以维护:
<code class="language-php">// Complex order calculation (using BCMath functions) // ... (code omitted for brevity)</code>
PHP 8.4 面向对象的 BCMath API 简化了精确计算:
<code class="language-php">use BCMath\Number; $item1 = Item::find(1); $price = new Number($item1->price); $quantity = new Number($item1->quantity); $discountRate = new Number($item1->discount); $taxRate = new Number($item1->tax); // Natural and readable calculations $subtotal = $price * $quantity; $discount = $subtotal * $discountRate; $afterDiscount = $subtotal - $discount; $tax = $afterDiscount * $taxRate; $total = $afterDiscount + $tax; var_dump($total); // Automatically converts to string</code>
新 API 的优点:
Stringable
接口实现。Laravel 的访问器实现了进一步的优雅:
<code class="language-php">use BCMath\Number; class Item extends Model { // ... (accessor methods for quantity, price, discount, tax using Number) ... }</code>
或者使用自定义演员表:
<code class="language-php">// ... (DecimalCast class implementation) ...</code>
然后:
<code class="language-php">$item1 = Item::find(1); $subtotal = $item1->price * $item1->quantity; // ... (rest of the calculation) ...</code>
在医疗保健库存管理中,精确的小数计算至关重要。 PHP 8.4 的 BCMath 对象 API 与 Laravel 集成,显着改进了这些计算的处理,提供精度、可读性、可维护性和类型安全性。 虽然旧的 BCMath 函数达到了其目的,但这种新方法大大简化了开发。
以上是使用新的 BCMath 对象 API 在 PHP 中处理小数计算的详细内容。更多信息请关注PHP中文网其他相关文章!