php:int() 函数等效于 bigint 类型?(int() 将字符串剪切为 2147483647)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8101367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php: int() function equivalent for bigint type? (int() cuts strings to 2147483647)
提问by Haradzieniec
php: What's the equivalent int()
function for bigint type? (int()
cuts big numbers to 2147483647)?
php:int()
bigint 类型的等效函数是什么?(int()
将大数字削减为 2147483647)?
Example:
例子:
$bigint1="12312342306A_C243";
$bigint1=(int)$bigint1;//2147483647
but I want it to be 12312342306.
但我希望它是 12312342306。
采纳答案by Aurelio De Rosa
There isn't a built-in type to do this kind of cast as you can see here(from the official doc). Anyway, you can use the GMP libraryto manage this long int.
正如您在此处看到的(来自官方文档),没有内置类型可以执行此类转换。无论如何,您可以使用GMP 库来管理这个 long int。
回答by Manatax
I know is old and answered, but for future reference of people looking at this:
我知道是旧的并且已经回答了,但供将来查看此内容的人参考:
UPDATED
更新
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, maximum value using the constant PHP_INT_MAX since PHP 5.0.5, and minimum value using the constant PHP_INT_MIN since PHP 7.0.0.
整数的大小取决于平台,但通常的值(即 32 位有符号)约为 20 亿的最大值。64 位平台的最大值通常约为 9E18,但在 PHP 7 之前的 Windows 上,它始终为 32 位。PHP 不支持无符号整数。整数大小可以使用常量 PHP_INT_SIZE 确定,最大值使用常量 PHP_INT_MAX 自 PHP 5.0.5 以来,最小值使用常量 PHP_INT_MIN 自 PHP 7.0.0 以来。
回答by Menno Bieringa
I solved it by casting to float rather than int:
我通过强制转换为 float 而不是 int 来解决它:
(int) '8554104470' => 2147483647
(int) '8554104470' => 2147483647
(float) '8554104470' => 8554104470
(浮点数)'8554104470' => 8554104470
回答by tialaramex
This may not be the kind of answer you wanted, but, why not switch to a 64-bit machine?
这可能不是您想要的那种答案,但是,为什么不切换到 64 位机器呢?
On my (64-bit Fedora) PC $bigint1 has the value 12312342306 as you desired.
在我的(64 位 Fedora)PC 上,$bigint1 的值是 12312342306。
回答by Frank Farmer
It's obviously not possibleto replicate the function of (int)
exactly for 64-bit numbers on a 32-bit system. (int) returns an int; the best you can do on a 32-bit system for 64-bit numbers is return a string -- but presumably this is really what you're asking for.
显然不可能(int)
在 32 位系统上完全复制64 位数字的功能。(int) 返回一个整数;对于 64 位数字,在 32 位系统上可以做的最好的事情是返回一个字符串——但大概这确实是你所要求的。
As tialaramex pointed out, if you're going to do a lot of work with 64 bit integers, you should run 64 bit PHP. If that's not an option, you can do some work using the bcmath functions-- although I don't see a bcmath function for doing what you've asked.
正如 tialaramex 指出的那样,如果您要使用 64 位整数进行大量工作,则应该运行 64 位 PHP。如果这不是一个选项,您可以使用bcmath 函数做一些工作——尽管我没有看到 bcmath 函数可以完成您的要求。
I'd just write a quick and dirty function:
我只想写一个快速而肮脏的函数:
<?php
var_dump(toint("12312342306A_C243"));
function toint($str) {
return preg_match('/^[0-9]+/', $str, $matches) ? $matches[0] : 0;
}
回答by Chemaclass
$bigint1="12312342306A_C243";
$bigint1=(int)$bigint1;//2147483647
If you want just the number until the first char which is not a number( as you mention you just want 12312342306
) then I suggest just cut that string, take what you want, and just then cast it into int
.
如果你只想要数字直到第一个不是数字的字符(正如你提到的你只是想要12312342306
),那么我建议只剪掉那个字符串,取你想要的,然后把它转换成int
.
function parseBigInt(string $bigInt, string $delimiter = '_'): int
{
if (false !== ($pos = strpos($bigInt, $delimiter))) {
$bigInt = substr($bigInt, 0, $pos);
}
return (int)$bigInt;
}
$bigInt = parseBigInt('12312342306A_C243'); // '12312342306' as int
回答by Hink
I am using bcmath functionsfor all operations with integer of size > 4 bytes. You must keep in mind, all operands and results are strings, but easy usable for displaying or passing to database as argument values.
我将bcmath 函数用于所有大小大于 4 字节的整数的操作。您必须记住,所有操作数和结果都是字符串,但很容易用于显示或作为参数值传递到数据库。