Core\BigInt::toInt
$bigInt->toInt(): intReturns this number as an int, when it fits in an int.
A Core\BigInt has no size limit. An int does: it stores whole numbers from -9223372036854775808 to 9223372036854775807. toInt returns the int equal to this number, with the same sign and the same value. When the number is outside that range, toInt throws an ArithmeticError. It never cuts a number down to fit, so a result you get back is always exact.
This is how a large calculation comes back to a plain number, for a count, a loop or a column in a database. Catch the ArithmeticError wherever the number may be too large, and use Core\BigInt::toString instead where you only need to print it.
Good to know: Core\BigInt::of converts the other way, and it accepts every int. A number that came from an int and only grew smaller always converts back with toInt.
Return value
Returns int. The int equal to this value.
Errors
ArithmeticErrorThis value is outside theintrange.
Examples
A number as an int
<?nvs// Shows how to convert a `Core\BigInt` back to an `int`.
// `toInt` returns the `int` equal to this number. This prints 42.echo Core\BigInt::of(42)->toInt(), "\n";
// The sign is kept. This prints -7.echo Core\BigInt::parse('-7')->toInt(), "\n";
// The result is an `int`, so you can do arithmetic with it. This prints 1000.int $count = Core\BigInt::of(500)->toInt();echo $count * 2, "\n";
// The largest number an `int` holds. This prints 9223372036854775807.echo Core\BigInt::parse('9223372036854775807')->toInt(), "\n";42-710009223372036854775807When the number is too large
<?nvs// Shows what `toInt` does with a number an `int` cannot hold. An `int` stops at// 9223372036854775807.
var $value = Core\BigInt::parse('9223372036854775808');
// `toInt` throws an `ArithmeticError`. The message names the method and the// number it refused.try { echo $value->toInt(), "\n";} catch (ArithmeticError $tooLarge) { echo $tooLarge->message, "\n";}
// The number itself is unchanged, and `toString` prints all of it.echo $value->toString(), "\n";
// The same happens below zero. An `int` stops at -9223372036854775808.try { echo Core\BigInt::parse('-9223372036854775809')->toInt(), "\n";} catch (ArithmeticError $tooSmall) { echo $tooSmall->message, "\n";}Core\BigInt::toInt(): 9223372036854775808 is outside the `int` range9223372036854775808Core\BigInt::toInt(): -9223372036854775809 is outside the `int` rangeStoring a total as an int
<?nvs// Shows how a program adds up amounts that arrive as text and stores the total// as an `int`. The amounts are in cents, and the database column holds an// `int`, so the total has to fit before the program can store it.
array<string> $amounts = ['4500000000', '129900000000', '77000'];
var $total = Core\BigInt::of(0);foreach ($amounts as string $amount) { $total = $total->plus(Core\BigInt::parse($amount));}
// This total fits an `int`, so this prints 134400077000.try { echo $total->toInt(), "\n";} catch (ArithmeticError $tooLarge) { echo "total is too large to store\n";}
// A total a billion times larger does not fit, and the program reports it// instead of storing a wrong number. This prints the second message.var $huge = $total->multipliedBy(Core\BigInt::of(1000000000));try { echo $huge->toInt(), "\n";} catch (ArithmeticError $tooLarge) { echo "total is too large to store\n";}134400077000total is too large to store