是否有一些方法告诉PHP使用标准/天然语法使用适当的精确浮子/小数?
我发现必须记住使用,然后实际使用各种bc*
函数,而不是+
, - ,这很烦人和繁琐。 ,*
,/
等,只要涉及金钱或任何重要的事情。
如果您不了解我在说什么,则PHP基本上使用“不准确” float
s 用于其内置的天然语法,显然,大多数其他编程语言也可以为速度目的而使用。 PHP通过提供必须使用的函数来获得准确的小数数字来“修复”此功能。
但是,嵌套函数调用是如此丑陋:
$leftovers = bcsub($account_balance, $total_cost_for_products);
而不仅仅是干净:
$leftovers = $account_balance - $total_cost_for_products;
真的没有办法做这样的事情吗?
php_ini('full_accuracy_decimals_mode', true);
$leftovers = $account_balance - $total_cost_for_products;
[...]
php_ini('full_accuracy_decimals_mode', false);
?
I find it very annoying and cumbersome to have to remember to use, and then actually use, the various bc*
functions instead of +
, -
, *
, /
, etc., whenever money or anything important is involved.
If you don't understand what I'm talking about, PHP basically uses "inaccurate" float
s for its built-in, natural syntax, which apparently most other programming languages also do, for speed purposes. PHP "fixes" this by providing functions that you must use instead to get accurate decimal numbers.
But it's so ugly to have nested function calls:
$leftovers = bcsub($account_balance, $total_cost_for_products);
instead of just a clean:
$leftovers = $account_balance - $total_cost_for_products;
Is there really no way to do something like this?
php_ini('full_accuracy_decimals_mode', true);
$leftovers = $account_balance - $total_cost_for_products;
[...]
php_ini('full_accuracy_decimals_mode', false);
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论