parseDouble method

double parseDouble({
  1. int? decimalDigits,
})

Implementation

double parseDouble({int? decimalDigits}) {
  try {
    if (isNullOrEmptyOrZero()) {
      return decimalDigits == null
          ? 0
          : double.parse(0.toStringAsFixed(decimalDigits));
    }

    if (this is double) {
      final d = this as double;
      return decimalDigits == null
          ? d
          : double.parse(d.toStringAsFixed(decimalDigits));
    }

    if (this is int) {
      final i = this as int;
      return decimalDigits == null
          ? i.toDouble()
          : double.parse(i.toDouble().toStringAsFixed(decimalDigits));
    }

    if (this is bool) return (this as bool) ? 1.0 : 0.0;

    final str = toString().trim();
    if (str.equals("+") || str.equals("-") || str.equals(".")) {
      return decimalDigits == null
          ? 0
          : double.parse(0.toStringAsFixed(decimalDigits));
    }

    final parsed = double.parse(str);
    return decimalDigits == null
        ? parsed
        : double.parse(parsed.toStringAsFixed(decimalDigits));
  } catch (e) {
    return 0;
  }
}