compareTo method

  1. @override
int compareTo(
  1. Decimal other
)
override

Compares this object to another object.

Returns a value like a Comparator when comparing this to other. That is, it returns a negative integer if this is ordered before other, a positive integer if this is ordered after other, and zero if this and other are ordered together.

The other argument must be a value that is comparable to this object.

Implementation

@override
int compareTo(Decimal other) {
  if (coefficient.sign != other.coefficient.sign) {
    return coefficient.sign.compareTo(other.coefficient.sign);
  }
  if (coefficient == BigInt.zero) return 0;
  final size = coefficient.abs().toString().length - scale;
  final otherSize = other.coefficient.abs().toString().length - other.scale;
  if (size != otherSize) return size.compareTo(otherSize) * coefficient.sign;
  final common = math.max(scale, other.scale);
  return (coefficient * _power(common - scale)).compareTo(
    other.coefficient * _power(common - other.scale),
  );
}