Core\BigInt::toUint
$bigInt->toUint(): uintReturns this number as a uint, when it fits in a uint.
A uint stores whole numbers from 0 to 18446744073709551615. It reaches one bit further up than an int does, and it has no negative numbers at all. toUint returns the uint equal to this number. When the number is negative, or above that range, toUint throws an ArithmeticError. It never cuts a number down to fit, so a result you get back is always exact.
Use toUint for a value that cannot be negative: a size, a count, a length, an identifier. Where a number may be negative, test Core\BigInt::sign first, or use Core\BigInt::toInt, which accepts negative numbers and stops one bit lower.
Good to know: the range of a uint covers every 64-bit identifier a database or another service can give you. Core\BigInt::parse reads such an identifier from text of any length.
Return value
Returns uint. The uint equal to this value.
Errors
ArithmeticErrorThis value is negative or above theuintrange.
Examples
A number as a uint
<?nvs// Shows how to convert a `Core\BigInt` to a `uint`, which holds whole numbers// from 0 upwards.
// `toUint` returns the `uint` equal to this number. This prints 42.echo Core\BigInt::of(42)->toUint(), "\n";
// The result is a `uint`, so you can use it where a count is needed. This// prints 84.uint $count = Core\BigInt::of(42)->toUint();echo $count * 2, "\n";
// A `uint` reaches one bit further up than an `int` does. This prints// 18446744073709551615, the largest number a `uint` holds.echo Core\BigInt::parse('18446744073709551615')->toUint(), "\n";428418446744073709551615Negative and too large numbers
<?nvs// Shows the two numbers `toUint` does not accept: a negative one, and one above// 18446744073709551615.
// A `uint` holds no negative numbers, so `toUint` throws an `ArithmeticError`.// The message names the method and the number it refused.try { echo Core\BigInt::of(-1)->toUint(), "\n";} catch (ArithmeticError $negative) { echo $negative->message, "\n";}
// The first number above the range is refused in the same way.try { echo Core\BigInt::parse('18446744073709551616')->toUint(), "\n";} catch (ArithmeticError $tooLarge) { echo $tooLarge->message, "\n";}
// `Core\BigInt::sign` returns -1 for a negative number, so you can test a// number before you convert it. This prints "negative".var $value = Core\BigInt::parse('-5000');echo $value->sign() < 0 ? "negative\n" : "zero or more\n";Core\BigInt::toUint(): -1 is outside the `uint` rangeCore\BigInt::toUint(): 18446744073709551616 is outside the `uint` rangenegativeReading account identifiers
<?nvs// Shows how a program reads 64-bit identifiers that arrive as text. An// identifier is never negative, and the largest ones are above what an `int`// holds, so `uint` is the type that fits them.
array<string> $ids = ['1042', '18446744073709551615', '-3'];
foreach ($ids as string $id) { var $number = Core\BigInt::parse($id); // The first two identifiers are printed. The third is negative, so the // program reports it instead of using it. try { uint $account = $number->toUint(); echo 'account ', $account, "\n"; } catch (ArithmeticError $notAnId) { echo 'not an identifier: ', $id, "\n"; }}account 1042account 18446744073709551615not an identifier: -3