5

在 PHP 中对表示实数的字符串进行基本算术运算的最快和最简单的方法是什么?我有一个代表 MySQL 的 DECIMAL 值的字符串,我想对其进行操作并将结果返回到数据库。我想避免浮点实数表示引入的错误。

有没有像 Python 的十进制这样的标准库?您可以推荐任何 FOSS 库吗?

问候

4

2 回答 2

5

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

于 2010-12-21T18:28:50.663 回答
3

You can use the bc_math extension, which works exactly how you ask (it's used for arbitrary precision numbers):

$num = '123.456';
$twoNum = bcadd($num, $num);
于 2010-12-21T18:28:49.597 回答