Core\BigInt::parse
Core\BigInt::parse(string $text, {radix?: uint}): Core\BigIntCore\BigInt::parse( string $text, {radix?: uint}, ): Core\BigInt
Reads a Core\BigInt out of text.
parse reads the whole text as a whole number: an optional - or +, and then digits. There is no limit on how many digits there are. Nothing else is allowed, so a space, a _ separator or a 0x prefix makes it throw a ParseError.
The digits are read in base 10 unless you set the radix option. That option may be any base from 2 to 36, where the digits after 9 are the letters a to z. Core\BigInt::format writes a number back in any base parse reads, so a value can be stored as text and read again exactly.
This is how a number that arrived as text becomes a number your program can add or multiply: a field of a JSON body, a line of a report, a hexadecimal hash. For a number your program already has as an int, use Core\BigInt::of.
Parameters
$textstringThe whole text to read: an optional-or+, then digits, and nothing else. Text before the number is not skipped._separators are not allowed: you can use them only in a number written directly in the code.
Options
radixuintoptionalThe base the digits are written in, from 2 to 36. The lettersatozare the digits after9. The default is 10.
Return value
Returns Core\BigInt. A BigInt equal to the number in $text.
Errors
ParseError$textis not an integer in that base. It is empty, contains a separator, or contains a digit the base does not have.LogicErrorradixis outside 2 to 36.
Examples
Reading a number from text
<?nvs// Shows how to read a whole number out of text, in any size.
// `parse` reads the text as a number written in base 10. This prints 4500.echo Core\BigInt::parse('4500'), "\n";
// A leading `-` or `+` is allowed. This prints -4500 and 4500.echo Core\BigInt::parse('-4500'), "\n";echo Core\BigInt::parse('+4500'), "\n";
// The text may hold a number far larger than an `int`. This prints the same// 40 digits back.echo Core\BigInt::parse('1234567890123456789012345678901234567890'), "\n";4500-450045001234567890123456789012345678901234567890Reading a number written in another base
<?nvs// Shows how to read a number that is written in another base, such as the// hexadecimal of a colour or a hash.
// The `radix` option is the base the digits are written in. 16 is hexadecimal,// where `a` to `f` are the digits after 9. This prints 255.echo Core\BigInt::parse('ff', { radix: 16 }), "\n";
// Base 2 reads binary digits. This prints 10.echo Core\BigInt::parse('1010', { radix: 2 }), "\n";
// The base may be any number from 2 to 36. This prints 1295.echo Core\BigInt::parse('zz', { radix: 36 }), "\n";
// `format` writes a number in any base `parse` reads, so the two go together.// This prints ff.echo Core\BigInt::parse('255')->format({ radix: 16 }), "\n";255101295ffAdding up amounts that arrived as text
<?nvs// Shows a program adding up amounts that arrived as text, the way one that// reads a report file does. Each amount is larger than an `int` holds.
class Report{ // Returns the total of the amounts. A line that is not a number is // reported and skipped. public static function total(array<string> $lines): Core\BigInt { var $total = Core\BigInt::of(0); foreach ($lines as string $line) { try { $total = $total->plus(Core\BigInt::parse($line)); } catch (ParseError $bad) { echo 'skipped: ', $bad->message, "\n"; } } return $total; }}
array<string> $lines = [ '170141183460469231731687303715884105727', '170141183460469231731687303715884105727', '1 000',];
// This prints the skipped line first, then the total of the other two.echo Report::total($lines), "\n";skipped: Core\BigInt::parse(): `1 000` is not an integer in radix 10340282366920938463463374607431768211454