applyDecimalsOption function

({int decimals, BigInt raw}) applyDecimalsOption(
  1. BigInt raw,
  2. int currentDecimals, [
  3. FixedPointToStringOptions options = const FixedPointToStringOptions()
])

Applies the configured target decimal count to a base-10 scaled integer.

Implementation

({int decimals, BigInt raw}) applyDecimalsOption(
  BigInt raw,
  int currentDecimals, [
  FixedPointToStringOptions options = const FixedPointToStringOptions(),
]) {
  final targetDecimals = options.decimals;

  if (targetDecimals == null || targetDecimals == currentDecimals) {
    return (decimals: currentDecimals, raw: raw);
  }

  if (targetDecimals < 0) {
    throw RangeError.range(targetDecimals, 0, null, 'decimals');
  }

  if (targetDecimals > currentDecimals) {
    return (
      decimals: targetDecimals,
      raw: raw * _pow10(targetDecimals - currentDecimals),
    );
  }

  return (
    decimals: targetDecimals,
    raw: _divideWithRounding(
      raw,
      _pow10(currentDecimals - targetDecimals),
      options.rounding,
    ),
  );
}