mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Add static Decimal.compare method
This commit is contained in:
parent
d6d6d3fee5
commit
9ac1994cb6
@ -64,6 +64,11 @@ export class Decimal {
|
||||
}
|
||||
}
|
||||
|
||||
public static compare(a: Decimal, b: Decimal): number {
|
||||
if (a.fractionalDigits !== b.fractionalDigits) throw new Error("Fractional digits do not match");
|
||||
return a.data.atomics.cmp(new BN(b.atomics));
|
||||
}
|
||||
|
||||
public get atomics(): string {
|
||||
return this.data.atomics.toString();
|
||||
}
|
||||
@ -119,28 +124,23 @@ export class Decimal {
|
||||
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;
|
||||
return Decimal.compare(this, b) === 0;
|
||||
}
|
||||
|
||||
public isLessThan(b: Decimal): boolean {
|
||||
return this.compare(b) < 0;
|
||||
return Decimal.compare(this, b) < 0;
|
||||
}
|
||||
|
||||
public isLessThanOrEqual(b: Decimal): boolean {
|
||||
return this.compare(b) <= 0;
|
||||
return Decimal.compare(this, b) <= 0;
|
||||
}
|
||||
|
||||
public isGreaterThan(b: Decimal): boolean {
|
||||
return this.compare(b) > 0;
|
||||
return Decimal.compare(this, b) > 0;
|
||||
}
|
||||
|
||||
public isGreaterThanOrEqual(b: Decimal): boolean {
|
||||
return this.compare(b) >= 0;
|
||||
return Decimal.compare(this, b) >= 0;
|
||||
}
|
||||
}
|
||||
|
2
packages/math/types/decimal.d.ts
vendored
2
packages/math/types/decimal.d.ts
vendored
@ -7,6 +7,7 @@ export declare class Decimal {
|
||||
static fromUserInput(input: string, fractionalDigits: number): Decimal;
|
||||
static fromAtomics(atomics: string, fractionalDigits: number): Decimal;
|
||||
private static verifyFractionalDigits;
|
||||
static compare(a: Decimal, b: Decimal): number;
|
||||
get atomics(): string;
|
||||
get fractionalDigits(): number;
|
||||
private readonly data;
|
||||
@ -23,7 +24,6 @@ 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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user