Add comparison methods to Decimal

This commit is contained in:
willclarktech 2020-06-10 13:40:42 +01:00
parent 62db625dbe
commit 8fbb7d4bcc
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
2 changed files with 31 additions and 0 deletions

View File

@ -118,4 +118,29 @@ export class Decimal {
const sum = this.data.atomics.add(new BN(b.atomics));
return new Decimal(sum.toString(), this.fractionalDigits);
}
public compare(b: Decimal): number {
if (this.fractionalDigits !== b.fractionalDigits) throw new Error("Fractional digits do not match");
return this.data.atomics.cmp(new BN(b.atomics));
}
public equals(b: Decimal): boolean {
return this.compare(b) === 0;
}
public isLessThan(b: Decimal): boolean {
return this.compare(b) < 0;
}
public isLessThanOrEqual(b: Decimal): boolean {
return this.compare(b) <= 0;
}
public isGreaterThan(b: Decimal): boolean {
return this.compare(b) > 0;
}
public isGreaterThanOrEqual(b: Decimal): boolean {
return this.compare(b) >= 0;
}
}

View File

@ -23,4 +23,10 @@ export declare class Decimal {
* Both values need to have the same fractional digits.
*/
plus(b: Decimal): Decimal;
compare(b: Decimal): number;
equals(b: Decimal): boolean;
isLessThan(b: Decimal): boolean;
isLessThanOrEqual(b: Decimal): boolean;
isGreaterThan(b: Decimal): boolean;
isGreaterThanOrEqual(b: Decimal): boolean;
}