php: int() function equivalent for bigint type? (int() cuts strings to 2147483647)
Solution 1
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 library to manage this long int.
Solution 2
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.
Solution 3
I solved it by casting to float rather than int:
(int) '8554104470' => 2147483647
(float) '8554104470' => 8554104470
Solution 4
This may not be the kind of answer you wanted, but, why not switch to a 64-bit machine?
On my (64-bit Fedora) PC $bigint1 has the value 12312342306 as you desired.
Solution 5
It's obviously not possible to 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.
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.
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;
}
Haradzieniec
Updated on November 08, 2020Comments
-
Haradzieniec over 3 years
php: What's the equivalent
int()
function for bigint type? (int()
cuts big numbers to 2147483647)?Example:
$bigint1="12312342306A_C243"; $bigint1=(int)$bigint1;//2147483647
but I want it to be 12312342306.
-
Frank Farmer over 12 yearsIs there a specific GMP function that will do what the OP asked?
-
Aurelio De Rosa over 12 years@FrankFarmer It really depends on what the OP wants to do with that number. In his question, he only shows a cast. Anyway, to manage that kind of int, GMP is a good library to use (just like BCMath).