Decimal.parse constructor
Decimal.parse(
- String source
Parses finite decimal text, including scientific notation.
Invalid syntax throws FormatException; unsupported magnitude or scale throws RangeError. No intermediate floating-point value is used.
Implementation
factory Decimal.parse(String source) {
// Bound parsing work before allocating a potentially enormous BigInt.
if (source.length > maxIntegerDigits + maxFractionDigits + 32) {
throw const FormatException('Decimal input is too long.');
}
final match = _syntax.firstMatch(source);
if (match == null ||
match.end != source.length ||
(match[2]!.isEmpty && (match[3] ?? '').isEmpty)) {
throw const FormatException('Expected a finite decimal string.');
}
final exponent = int.tryParse(match[4] ?? '0');
if (exponent == null ||
exponent < -maxIntegerDigits - maxFractionDigits ||
exponent > maxIntegerDigits + maxFractionDigits) {
throw const FormatException('Decimal exponent is out of range.');
}
final fraction = match[3] ?? '';
return Decimal._digits(
'${match[2]}$fraction',
match[1] == '-',
fraction.length - exponent,
);
}