parseNum function

num? parseNum(
  1. Object? v, [
  2. num? def
])

Parses v to num.

def The default value if v is invalid.

Implementation

num? parseNum(Object? v, [num? def]) {
  if (v == null) return def;

  if (v is num) return v;

  if (v is DateTime) return v.millisecondsSinceEpoch;

  String s;
  if (v is String) {
    s = v;
  } else {
    s = v.toString();
  }

  s = s.trim();

  if (s.isEmpty) return def;

  var n = num.tryParse(s);
  return n ?? def;
}