Floating-point arithmetic involves representing fractional numbers as binary fractions, which may not have exact representations. This can lead to rounding errors.
Can this workaround ensure precise 2-digit calculations for money?
No. PHP precision settings cannot guarantee precise 2-digit calculations, as precision can be increased during calculations.
Example of when the workaround fails:
ini_set('precision', 8); $a = 5.88; // cost of 1kg $q = 2.49; // User buys 2.49 kg $b = $a * 0.01; // 10% Discount only on first kg echo ($a * $q) - $b; // result: 14.5824 (not precise for money calculations)
Which php.ini.precision value suits best for 2-digit money calculations?
PHP precision is not a recommended metric for precise financial calculations.
Using bcmath, number_format, and subtraction:
$a = 342349.23; $b = 341765.07; ini_set('precision', 20); echo $a - $b, PHP_EOL; // 584.15999999997438863 echo floatval(round($a - $b, 2)), PHP_EOL; // 584.15999999999996817 (rounding issue) echo number_format($a - $b, 2), PHP_EOL; // 584.16 echo bcsub($a, $b, 2), PHP_EOL; // 584.15 (precision set to 20) ini_set('precision', 14); echo $a - $b, PHP_EOL; // 584.15999999997 echo floatval(round($a - $b, 2)), PHP_EOL; // 584.16 echo number_format($a - $b, 2), PHP_EOL; // 584.16 echo bcsub($a, $b, 2), PHP_EOL; // 584.16 (precision set to 14) ini_set('precision', 6); echo $a - $b, PHP_EOL; // 584.16 echo floatval(round($a - $b, 2)), PHP_EOL; // 584.16 echo number_format($a - $b, 2), PHP_EOL; // 584.16 echo bcsub($a, $b, 2), PHP_EOL; // 584.00 (precision set to 6) ini_set('precision', 3); echo $a - $b, PHP_EOL; // 584 echo floatval(round($a - $b, 2)), PHP_EOL; // 584 echo number_format($a - $b, 2), PHP_EOL; // 584.16 echo bcsub($a, $b, 2), PHP_EOL; // 0.00 (precision set to 3)
Using number_format for money calculations is recommended for consistency. Storing in cents is also an option.
Yes, due to the large number of combinations, a full test is impractical.
$a = 0.19; $b = 0.16; ... $h = $a + $b + ...; // Total value: 0.4 $i = $h - $a - $b - ...; // Result: 1.0408341E-17 (expected: 0.00)
Mathematical solutions exist, but it's not within the scope of this discussion.
The above is the detailed content of Is PHP's `precision` Workaround Reliable for Precise 2-Digit Money Calculations?. For more information, please follow other related articles on the PHP Chinese website!