parseDouble static method

double? parseDouble(
  1. dynamic value, [
  2. double? defaultValue
])

Parses the dynamic value into a double. The value may be a String, int, or double. If the value cannot be successfully parsed into a double then the defaultValue will be returned.

A value of the string "infinity" will result in double.infinity.

Implementation

static double? parseDouble(
  dynamic value, [
  double? defaultValue,
]) {
  double? result;
  try {
    if (value is String) {
      if (value.toLowerCase() == 'infinity') {
        result = double.infinity;
      } else if (value.startsWith('0x') == true) {
        result = int.tryParse(value.substring(2), radix: 16)?.toDouble();
      } else {
        result = double.tryParse(value);
      }
    } else if (value is double) {
      result = value;
    } else if (value is int) {
      result = value.toDouble();
    }
  } catch (e, stack) {
    _logger.finest('Error parsing: $value', e, stack);
  }

  return result ?? defaultValue;
}