fixed-precision
Version:
A fixed-precision decimal arithmetic library for JavaScript/TypeScript
87 lines (85 loc) • 3.6 kB
TypeScript
type RoundingMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
type Comparison = -1 | 0 | 1;
type FixedPrecisionValue = string | number | bigint | FixedPrecision;
type FPContext = {
places: number;
roundingMode: RoundingMode;
SCALE: bigint;
SCALENUMBER: number;
};
interface FixedPrecisionConfig {
places: number;
roundingMode?: RoundingMode;
}
declare class FixedPrecision {
private value;
private readonly ctx;
private static defaultContext;
static configure(config: FixedPrecisionConfig): void;
static create(config: FixedPrecisionConfig): (val: FixedPrecisionValue) => FixedPrecision;
constructor(value: FixedPrecisionValue, ctx?: FPContext);
protected fromRaw(rawValue: bigint): FixedPrecision;
private static toScaled;
private toScaledValue;
private coerce;
static random(decimalPlaces?: number): FixedPrecision;
private static resolveContext;
private static normalizeTo;
static min(value: FixedPrecisionValue | FixedPrecisionValue[], ...values: FixedPrecisionValue[]): FixedPrecision;
static max(value: FixedPrecisionValue | FixedPrecisionValue[], ...values: FixedPrecisionValue[]): FixedPrecision;
static sum(value: FixedPrecisionValue | FixedPrecisionValue[], ...values: FixedPrecisionValue[]): FixedPrecision;
abs(): FixedPrecision;
cmp(other: FixedPrecisionValue): Comparison;
eq(other: FixedPrecisionValue): boolean;
gt(other: FixedPrecisionValue): boolean;
gte(other: FixedPrecisionValue): boolean;
lt(other: FixedPrecisionValue): boolean;
lte(other: FixedPrecisionValue): boolean;
isInteger(): boolean;
isNegative(): boolean;
isPositive(): boolean;
isZero(): boolean;
add(other: FixedPrecisionValue): FixedPrecision;
plus(other: FixedPrecisionValue): FixedPrecision;
sub(other: FixedPrecisionValue): FixedPrecision;
minus(other: FixedPrecisionValue): FixedPrecision;
mul(other: FixedPrecisionValue): FixedPrecision;
tiems(other: FixedPrecisionValue): FixedPrecision;
div(other: FixedPrecisionValue): FixedPrecision;
idiv(other: FixedPrecisionValue): FixedPrecision;
ratio(other: FixedPrecisionValue): FixedPrecision;
mod(other: FixedPrecisionValue): FixedPrecision;
rem(other: FixedPrecisionValue): FixedPrecision;
divmod(other: FixedPrecisionValue): {
quotient: FixedPrecision;
remainder: FixedPrecision;
};
idivmod(other: FixedPrecisionValue): {
quotient: FixedPrecision;
remainder: FixedPrecision;
};
rest(other: FixedPrecisionValue): FixedPrecision;
neg(): FixedPrecision;
pow(exp: number): FixedPrecision;
square(): FixedPrecision;
sqrt(): FixedPrecision;
round(dp?: number, rm?: RoundingMode): FixedPrecision;
scale(newScale: number, rm?: RoundingMode): FixedPrecision;
prec(sd: number, rm?: RoundingMode): FixedPrecision;
trunc(): FixedPrecision;
ceil(): FixedPrecision;
floor(): FixedPrecision;
shiftedBy(n: number): FixedPrecision;
toNumber(places?: number): number;
toString(trimZeros?: boolean): string;
toFixed(places?: number, rm?: RoundingMode): string;
toExponential(dp?: number, rm?: RoundingMode): string;
toPrecision(sd: number, rm?: RoundingMode): string;
toFormat(dp?: number, rm?: RoundingMode): string;
toJSON(): string;
valueOf(): string;
}
declare const fixedconfig: {
configure: typeof FixedPrecision.configure;
};
export { type Comparison, type FixedPrecisionConfig, type FixedPrecisionValue, type RoundingMode, FixedPrecision as default, fixedconfig };