ext-decimal icon indicating copy to clipboard operation
ext-decimal copied to clipboard

Add helper functions

Open kloining opened this issue 5 years ago • 4 comments

Since Decimal is a final class, we cannot extend the class with custom methods. compareTo() gives us a comparison feature, but it's taking quite some time to get our minds wrapped around it every time.

It would be great with some helper functions: greaterThan($other) lessThan($other) equalTo($other) greaterThanOrEqualTo($other) lessThanOrEqualTo($other)

kloining avatar Oct 26 '20 08:10 kloining

Would it be implausible to decorate the class for your use case?

class  CompareDecimal {
  private $decimal;
  public function __construct(Decimal $decimal)
  {
    $this->decimal = $decimal;
  }

  public function greaterThan($other): bool
  {
    return $this->decimal->compareTo($other) === 1;
  }

  public function lessThan($other): bool
  {
    return $this->decimal->compareTo($other) === -1;
  }

  public function equalTo($other): bool
  {
    return $this->decimal->compareTo($other) === 0;
  }

  public function greaterThanOrEqualTo($other): bool
  {
    return $this->greaterThan($other) || $this->equalTo($other);
  }

  public function lessThanOrEqualTo($other): bool
  {
    return $this->lessThan($other) || $this->equalTo($other);
  }

}

Acen avatar Oct 27 '20 01:10 Acen

This is a great suggestion, I will add it to the API.

rtheunissen avatar Oct 27 '20 05:10 rtheunissen

@Acen This will do the trick, but I believe it's smoother to just have it on the Decimal instance anyway, to avoid yet another complementing class. This class will do the trick for now, thanks, but when Decimal already has a compareTo() function (which includes the requested type of logic), I believe it's also logical to add the missing/complementing functions

kloining avatar Nov 03 '20 13:11 kloining

@rtheunissen Which release will this be included in?

kloining avatar Nov 03 '20 14:11 kloining