/** * @fileoverview High-precision decimal number implementation for Internet Object. * * This module provides RDBMS-compliant decimal arithmetic with controlled * precision, scale, and rounding behaviors. */ /** * Error class for Decimal-specific errors. * Thrown when precision/scale constraints are violated or invalid operations are attempted. */ declare class DecimalError extends Error { constructor(message: string); } /** * Decimal provides arbitrary-precision decimal arithmetic with RDBMS-compliant behavior. * * Features: * - BigInt-based internal representation for exact arithmetic * - Configurable precision (total digits) and scale (decimal places) * - RDBMS-standard arithmetic operations (add, sub, mul, div, mod) * - Multiple rounding modes: round-half-up, ceil, floor * - Scientific notation and various input format support * - Immutable: all operations return new Decimal instances * * Precision & Scale: * - Precision (M): Total number of significant digits * - Scale (D): Number of digits after the decimal point * - Integer digits = Precision - Scale * * @example * ```typescript * // Basic usage * const price = new Decimal("19.99"); * const quantity = new Decimal("3"); * const total = price.mul(quantity); // 59.97 * * // Explicit precision/scale * const amount = new Decimal("123.456", 6, 2); // 123.46 (rounded) * * // Arithmetic operations * const a = new Decimal("100.50"); * const b = new Decimal("25.25"); * console.log(a.add(b).toString()); // "125.75" * console.log(a.sub(b).toString()); // "75.25" * * // Rounding modes * const value = new Decimal("10.555", 5, 2); * console.log(value.round(4, 2).toString()); // "10.56" (half-up) * console.log(value.ceil(4, 2).toString()); // "10.56" * console.log(value.floor(4, 2).toString()); // "10.55" * ``` */ declare class Decimal { private readonly coefficient; private readonly exponent; private readonly precision; private readonly scale; /** * Constructs a Decimal instance by parsing the input value. * * Constructor behavior varies based on input type: * * 1. String input: * - If precision/scale not provided: Infers from string format * - If precision/scale provided: Validates and formats according to specs * Example: new Decimal("123.45") or new Decimal("123.45", 5, 2) * * 2. Decimal input (conversion): * - If precision/scale not provided: Inherits from source Decimal * - If precision/scale provided: Adjusts value to match new precision/scale * Example: new Decimal(existingDecimal) or new Decimal(existingDecimal, 5, 2) * * 3. Number input: * - Always requires precision and scale parameters * Example: new Decimal(123.45, 5, 2) * * @param value The value to initialize the Decimal with (string, number, or Decimal) * @param precision The total number of significant digits (M) * @param scale The number of digits after the decimal point (D) * @throws {DecimalError} If value format is invalid or precision/scale constraints are violated */ constructor(value: string | number | Decimal, precision?: number, scale?: number); /** * Resolves precision and scale values based on the input type. * For strings, infers from the string if not provided. * For Decimal instances, inherits from source if not provided. * For numbers, ensures both are provided. * * @private */ private resolvePrecisionAndScale; /** * Validates that scale is less than or equal to precision. * @private */ private validatePrecisionAndScale; /** * Initializes a Decimal from a string value. * @private */ private initFromString; /** * Initializes a Decimal from a number value. * @private */ private initFromNumber; /** * Initializes a Decimal from an existing Decimal instance. * @private */ private initFromDecimal; /** * Handles increasing the scale when converting from one Decimal to another. * @private */ private increaseScaleForDecimal; /** * Handles decreasing the scale when converting from one Decimal to another. * @private */ private decreaseScaleForDecimal; /** * Handles keeping the same scale when converting from one Decimal to another. * @private */ private sameScaleForDecimal; /** * Special rounding implementation specifically for the Decimal constructor. * Ensures correct rounding behavior for all cases including edge cases. * @private */ private static roundForDecimal; /** * Returns the number of significant digits (excluding sign). * @private */ private getTotalDigits; /** * Validates if a string is a valid decimal format. * @param str The string to validate. * @returns True if valid, else false. */ static isValidDecimal(str: string): boolean; /** * Parses the string into sign, integer part, and fractional part. * Handles scientific notation and normalization. * @param str The string to parse. * @returns An object containing sign, integerPart, and fractionalPart. */ static parseString(str: string): { sign: string; integerPart: string; fractionalPart: string; }; /** * Converts the input value to a Decimal instance. * @param value The value to convert to a Decimal. * @returns A Decimal instance. * @throws {DecimalError} If the value cannot be converted to a Decimal. */ static ensureDecimal(value: unknown): Decimal; /** * Converts the Decimal instance to a JavaScript Number. * @returns The numeric representation. * @throws {DecimalError} If the conversion results in infinity. */ toNumber(): number; /** * Compares this Decimal instance with another. * @param other The other Decimal to compare with. * @returns 1 if greater, -1 if less, 0 if equal. * @throws {DecimalError} If decimals have different precision or scale. */ compareTo(other: Decimal): number; /** * Compares the structure of this Decimal with another. * The structure is defined by precision and scale. * @param other The other Decimal to compare. * @returns True if the structure is the same, else false. */ compareStructure(other: Decimal): boolean; /** * Checks if this Decimal is equal to another. * @param other The other Decimal to compare with. * @returns True if equal, else false. */ equals(other: Decimal): boolean; /** * Checks if this Decimal is less than another. * @param other The other Decimal to compare with. * @returns True if less, else false. */ lessThan(other: Decimal): boolean; /** * Checks if this Decimal is greater than another. * @param other The other Decimal to compare with. * @returns True if greater, else false. */ greaterThan(other: Decimal): boolean; /** * Checks if this Decimal is less than or equal to another. * @param other The other Decimal to compare with. * @returns True if less than or equal, else false. */ lessThanOrEqual(other: Decimal): boolean; /** * Checks if this Decimal is greater than or equal to another. * @param other The other Decimal to compare with. * @returns True if greater than or equal, else false. */ greaterThanOrEqual(other: Decimal): boolean; /** * Returns the string representation of the Decimal. * @returns The normalized string representation. */ toString(): string; /** * Gets the total number of significant digits. * @returns The precision value. */ getPrecision(): number; /** * Gets the number of fractional digits. * @returns The scale value. */ getScale(): number; /** * Gets the exponent (same as scale for this implementation). * @returns The exponent value. */ getExponent(): number; /** * Gets the internal coefficient (scaled integer representation). * @returns The coefficient as a BigInt. */ getCoefficient(): bigint; /** * Gets the format pattern representing the precision and scale. * @returns A string representing the format pattern (e.g., "xxx.xx"). */ getFormatPattern(): string; /** * Converts this Decimal to a new Decimal with the specified precision and scale. * @param targetPrecision The total number of significant digits (M) * @param targetScale The number of digits after the decimal point (D) * @returns A new Decimal with the specified precision and scale * @throws {DecimalError} If conversion is not possible due to precision constraints */ convert(targetPrecision: number, targetScale: number): Decimal; /** * Rounds this Decimal to the specified precision and scale using round-half-up behavior. * @param targetPrecision The total number of significant digits (M) * @param targetScale The number of digits after the decimal point (D) * @returns A new Decimal with the specified precision and scale, rounded using round-half-up * @throws {DecimalError} If targetScale > targetPrecision or if parameters are invalid */ round(targetPrecision: number, targetScale: number): Decimal; /** * Rounds this Decimal to the specified precision and scale using ceiling behavior (always round up). * @param targetPrecision The total number of significant digits (M) * @param targetScale The number of digits after the decimal point (D) * @returns A new Decimal with the specified precision and scale, rounded using ceiling * @throws {DecimalError} If targetScale > targetPrecision or if parameters are invalid */ ceil(targetPrecision: number, targetScale: number): Decimal; /** * Rounds this Decimal to the specified precision and scale using floor behavior (always round down). * @param targetPrecision The total number of significant digits (M) * @param targetScale The number of digits after the decimal point (D) * @returns A new Decimal with the specified precision and scale, rounded using floor * @throws {DecimalError} If targetScale > targetPrecision or if parameters are invalid */ floor(targetPrecision: number, targetScale: number): Decimal; /** * Computes the modulo (remainder) of this Decimal by another Decimal. * Uses truncation toward zero for the quotient (RDBMS-like), so the remainder has the same sign as the dividend. * Result scale is max(scale1, scale2). * @param other The Decimal divisor * @returns A new Decimal representing (this % other) */ mod(other: Decimal): Decimal; /** * Adds this Decimal to another and returns a new Decimal. * The result will match the scale of the first operand (this), and will be rounded if necessary. * @param other The Decimal to add. * @returns A new Decimal representing the sum, rounded to this.scale. */ /** * Adds this Decimal to another and returns a new Decimal. * The result will match the scale of the first operand (this), and will be rounded if necessary. * @param other The Decimal to add. * @returns A new Decimal representing the sum, rounded to this.scale. */ /** * Adds this Decimal to another and returns a new Decimal. * The result will match the scale of the first operand (this), and will be rounded if necessary. * @param other The Decimal to add. * @returns A new Decimal representing the sum, rounded to this.scale. */ add(other: Decimal): Decimal; /** * Subtracts another Decimal from this and returns a new Decimal. * The result will match the scale of the first operand (this), and will be rounded if necessary. * @param other The Decimal to subtract. * @returns A new Decimal representing the difference, rounded to this.scale. */ sub(other: Decimal): Decimal; /** * Multiplies this Decimal by another and returns a new Decimal. * Uses utility functions for scale operations and rounding. * The result scale follows RDBMS standard: max(scale1, scale2). * @param other The Decimal to multiply by. * @returns A new Decimal representing the product with RDBMS-compliant scale. */ mul(other: Decimal): Decimal; /** * Divides this Decimal by another and returns a new Decimal. * The result will match the scale of the dividend (this), and will be rounded if necessary. * This behavior is consistent with industry standards and ensures predictable precision. * @param other The Decimal to divide by. * @returns A new Decimal representing the quotient, rounded to this.scale. */ div(other: Decimal): Decimal; /** * Returns the string representation of the Decimal for JSON serialization. */ toJSON(): string; } export { DecimalError, Decimal as default };