gpt4 book ai didi

php - 在php中将长度超过16位的int转换为字符串

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:19 24 4
gpt4 key购买 nike

是否有任何函数可以轻松地回显 15 位以上长的整数?我管理的唯一方法是这样的:

$num = 123456789012345;
$num = number_format($num);
$num = str_replace(',', '', $num);
echo $num;

但即使这样,它也只能精确到 17 位数字。在第 16 位数字之后,数字打印不准确(因为作为 float ,它开始变得不准确 - see here)。

EDIT: From the answers below I wrote ini_set('precision',40); and then echoed $num straight. All this did was to, simply put, not show the decimal point in the float number. And again after the 16th digit it starts getting inaccurate. I also tried the other suggestion of changing it into an array and then iterating through it with str_split($num); and again the numbers were inaccurate from the 17th digit on!

最简单的解决方案是将整数转换为字符串。我试过:

$num = (string)$num; 
//and
$num = strval($num);

但两者都没有改变任何东西,它们的行为就好像它们仍然是一个整数??

我的问题具体是为什么无法转换为字符串。有没有办法把数字变成字符串?谢谢

最佳答案

我能想到的唯一解决方案是更改 php.ini 中 float 的精度

ini_set('precision', 25);

我不知道你从哪里得到这些大数字,但我建议你看看 bc functions也是!

我最后想到的是使用 explode 函数将字符串拆分为数组并对其进行交互。

编辑:当所有建议都失败时,您唯一的选择是查看 BC Math和/或 GMP功能以及MoneyMath . BigInteger package 也应该可以做到这一点,它使用 GMP 和 BC。

关于php - 在php中将长度超过16位的int转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com