是否有一些方法告诉PHP使用标准/天然语法使用适当的精确浮子/小数?

发布于 2025-02-09 23:39:57 字数 677 浏览 1 评论 0原文

我发现必须记住使用,然后实际使用各种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" floats 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文