divide method

BigDecimal divide(
  1. BigDecimal other, [
  2. int? resultScale
])

Divides this BigDecimal by another.

other the divisor scale the scale of the result (default is max of operand scales + 10)

Implementation

BigDecimal divide(BigDecimal other, [int? resultScale]) {
  if (other._unscaledValue == BigInt.zero) {
    throw InvalidArgumentException('Division by zero');
  }

  resultScale ??= (_scale > other._scale ? _scale : other._scale) + 10;

  // Scale up the dividend to achieve desired precision
  int scaleDiff = resultScale + other._scale - _scale;
  BigInt scaledDividend = _unscaledValue * BigInt.from(10).pow(scaleDiff);

  BigInt quotient = scaledDividend ~/ other._unscaledValue;
  return BigDecimal._(quotient, resultScale);
}