在PHP中显示更多数字

问题描述 投票:3回答:4

比方说我有:

echo 1/3;

它只打印出0.33333333333333,我能获得更多数字吗?

php numbers digits
4个回答
6
投票

可以使用bcdiv

echo bcdiv(1, 3, 20);

第三个论点

用于设置结果中小数位后面的位数。您还可以使用bcscale()为所有函数设置全局默认比例。


3
投票

precision或其他配置位置编辑php.ini配置变量或使用ini_set()

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

即使我非常怀疑你真的需要那种精确度;-)

编辑

正如戈登所说:你迟早会在PHP中达到浮点精度限制(取决于指定的精度)。因此,如果您正在使用真正的高精度数学,那么更好的方法是使用BCMath Arbitrary Precision Mathematics扩展或GNU Multiple Precision扩展。


2
投票

您可能想要查看BC任意精度php库qazxsw poi


1
投票

设置是http://php.net/manual/en/book.bc.phpprecision

但是,除了调试之外,我不会使用它。看看http://es.php.net/manual/en/ini.core.php

© www.soinside.com 2019 - 2024. All rights reserved.