tryParse static method
- String input
Parses a string containing a number literal into a number.
Like parse, except that this function returns null
for invalid inputs
instead of throwing.
Examples:
var value = num.tryParse('2021'); // 2021
value = num.tryParse('3.14'); // 3.14
value = num.tryParse(' 3.14 \xA0'); // 3.14
value = num.tryParse('0.'); // 0.0
value = num.tryParse('.0'); // 0.0
value = num.tryParse('-1.e3'); // -1000.0
value = num.tryParse('1234E+7'); // 12340000000.0
value = num.tryParse('+.12e-9'); // 1.2e-10
value = num.tryParse('-NaN'); // NaN
value = num.tryParse('0xFF'); // 255
value = num.tryParse(double.infinity.toString()); // Infinity
value = num.tryParse('1f'); // null
Implementation
static num? tryParse(String input) {
String source = input.trim();
// TODO(lrn): Optimize to detect format and result type in one check.
return int.tryParse(source) ?? double.tryParse(source);
}