Parses a string containing a number literal into a number.
The method first tries to read the input
as integer (similar to int.parse without a radix). If that fails, it tries to parse the input
as a double (similar to double.parse). If that fails, too, it invokes onError
with input
, and the result of that invocation becomes the result of calling parse
.
If no onError
is supplied, it defaults to a function that throws a FormatException.
For any number n
, this function satisfies identical(n, num.parse(n.toString()))
(except when n
is a NaN double
with a payload).
The onError
parameter is deprecated and will be removed. Instead of num.parse(string, (string) { ... })
, you should use num.tryParse(string) ?? (...)
.
static num parse(String input, [@deprecated num onError(String input)]) { num result = tryParse(input); if (result != null) return result; if (onError == null) throw new FormatException(input); return onError(input); }
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-core/num/parse.html