mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Add comparison methods to Decimal
This commit is contained in:
parent
62db625dbe
commit
8fbb7d4bcc
@ -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;
|
||||
}
|
||||
}
|
||||
|
6
packages/math/types/decimal.d.ts
vendored
6
packages/math/types/decimal.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user