operator / method
Division, may return Rational if not exact decimal
Examples:
- Decimal.parse("10") / Decimal.parse("4") -> "2.5"
- Decimal.parse("1") / Decimal.parse("3") -> "1/3" (Rational representation)
- Decimal.parse("2") / Decimal.parse("6") -> "1/3" (Rational representation)
- Decimal.parse("1") / Decimal.parse("8") -> "0.125"
- Decimal.parse("1") / Decimal.parse("0") -> throws ArgumentError
Implementation
@override
operator /(Decimal other) {
if (other == Decimal.zero) {
throw ArgumentError('Divided by zero');
}
final otherRational = other.toRational();
final newNumerator = _numerator * otherRational._denominator;
final newDenominator = _denominator * otherRational._numerator;
return Decimal.fraction(newNumerator, newDenominator);
}