toNum function
Converts an object to a num. mirroring the same static method in the ConvertObject, providing alternative easy less code usage options.
- Converts numeric types and strings that represent valid numbers to num.
- If the object is
null
, throws aParsingException
with anullObject
error. - If the conversion to num fails, throws a
ParsingException
.
object
The object to be converted to a num.
mapKey
(Optional) Specifies the key to extract values from a Map object.
listIndex
(Optional) Specifies the index to extract elements from a List object.
Returns a num if conversion is successful.
Throws a ParsingException
if the conversion fails or the object is null
.
Example usage:
final object1 = 10;
final num1 = toNum(object1); // 10
final object2 = '5.5';
final num2 = toNum(object2); // 5.5
final object3 = true;
final num3 = toNum(object3); // 1
final object4 = 'abc';
final num4 = toNum(object4); // throws ParsingException
final object5 = null;
final num5 = toNum(object5); // throws ParsingException
Implementation
num toNum(
dynamic object, {
Object? mapKey,
int? listIndex,
String? format,
String? locale,
}) =>
ConvertObject.toNum(
object,
mapKey: mapKey,
listIndex: listIndex,
format: format,
locale: locale,
);