使用 PHP 进行精确的实数运算
在 PHP 中对表示实数的字符串进行基本算术运算的最快且最简单的方法是什么?我有一个代表 MySQL 的 DECIMAL 值的字符串,我想对其进行操作并将结果返回到数据库。我想避免浮点实数表示引入的错误。
有没有像Python的十进制这样的标准库?有没有可以推荐的 FOSS 库?
问候
What is fastest and most easy way of making basic arithmetic operations on strings representing real numbers in PHP? I have a string representing MySQL's DECIMAL value on which I want to operate and return the result to a database after that. I would like to avoid errors introduced by floating-point real number representation.
Is there some standard library like Python's decimal? Are there any FOSS libraries you can recommend?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 BC 数学函数:
http://www.php.net/manual /en/ref.bc.php
或者 GMP 函数,尽管它们似乎只是整数。
http://www.php.net/manual/en/ref.gmp.php
You could use the BC math functions:
http://www.php.net/manual/en/ref.bc.php
Or the GMP functions, although they seem to be integer only.
http://www.php.net/manual/en/ref.gmp.php
您可以使用
bc_math
扩展,它完全按照你的要求工作(它用于任意精度数字):You can use the
bc_math
extension, which works exactly how you ask (it's used for arbitrary precision numbers):