toDoubleValue function

double toDoubleValue(
  1. Object? data, {
  2. double? min,
  3. double? max,
})

Converts data to double or throws FormatException if cannot convert.

If provided min and max are used to clamp the returned value.

Implementation

double toDoubleValue(Object? data, {double? min, double? max}) {
  if (data == null) throw const NullValueException();
  double result;
  if (data is num) {
    result = data.toDouble();
  } else if (data is BigInt) {
    result = data.toDouble();
  } else if (data is String) {
    result = double.parse(data);
  } else if (data is bool) {
    result = data ? 1.0 : 0.0;
  } else {
    throw ConversionException(target: double, data: data);
  }
  if (min != null && result < min) {
    result = min;
  }
  if (max != null && result > max) {
    result = max;
  }
  return result;
}