tryFromNum static method

FdcDecimal? tryFromNum(
  1. num value, {
  2. int? scale,
})

Converts a finite num through its decimal text representation.

When scale is omitted, the scale is inferred from the number's decimal text representation. Pass scale to round and normalize the value to a fixed number of decimals.

Implementation

static FdcDecimal? tryFromNum(num value, {int? scale}) {
  if (!value.isFinite) {
    return null;
  }
  if (scale != null) {
    final normalized = FdcDecimalMath.normalizeNumberForParsing(
      value,
      scale: scale,
    );
    if (normalized == null) {
      return null;
    }
    return tryParseNormalized(normalized, scale: scale);
  }

  final text = _normalizedFiniteNumText(value);
  return tryParseNormalized(text, scale: _fractionScale(text));
}