开发者

Doing exact real number arithmetic with PHP

Wha开发者_如何学编程t 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


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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜