Show more digits in PHP

左心房为你撑大大i 提交于 2019-12-01 22:32:45

问题


Let's say I have:

echo 1/3;

And it print out only 0.33333333333333, can I get more digits?


回答1:


Can use bcdiv

echo bcdiv(1, 3, 20);

The third argument

is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale().




回答2:


Edit the precision configuration variable either in your php.ini or some other configuration location or use ini_set().

ini_set('precision', 22);
echo 1/3;
// 0.3333333333333333148296

Even though I highly doubt that you really need that kind of precision ;-)

EDIT

As Gordon said: you'll hit the floating point precision limit in PHP sooner or later (depending on the precision specified). So the better way would be to use either the BCMath Arbitrary Precision Mathematics extension or the GNU Multiple Precision extension, if you're after real high precision mathematics.




回答3:


You might want tto look into the BC arbitary precision php library http://php.net/manual/en/book.bc.php




回答4:


The setting is precision: http://es.php.net/manual/en/ini.core.php

However, I would not use it except for debugging purposes. Have a look at number_format()



来源:https://stackoverflow.com/questions/6492028/show-more-digits-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!