@tusss/ood
    Preparing search index...

    Class DecimalPrecision

    A utility class for handling decimal precision arithmetic.

    This helper offers more accurate results for rounding, ceiling, flooring, truncation, and fixed-point representation than standard JavaScript Math functions, which can suffer from binary floating-point precision issues.

    Basic usage of DecimalPrecision methods

    import { DecimalPrecision } from './decimal-precision';

    // Rounding
    DecimalPrecision.round(1.005, 2); // 1.01
    DecimalPrecision.round(1262.48, -1); // 1260

    // Ceiling
    DecimalPrecision.ceil(5.12, 1); // 5.2

    // Flooring
    DecimalPrecision.floor(5.12, 1); // 5.1

    // Truncating
    DecimalPrecision.trunc(-5.12, 1); // -5.1

    // Format to fixed string
    DecimalPrecision.toFixed(1.005, 2); // "1.01"
    Index

    Constructors

    Methods

    Constructors

    Methods

    • Computes the smallest integer greater than or equal to a number, resolved to a specified number of decimal places.

      Parameters

      • num: number

        The number to evaluate.

      • decimalPlaces: number = 0

        The number of decimal places to resolve to. Defaults to 0.

      Returns number

      The ceiling value of the number resolved to the specified decimal places.

    • Computes the largest integer less than or equal to a number, resolved to a specified number of decimal places.

      Parameters

      • num: number

        The number to evaluate.

      • decimalPlaces: number

        The number of decimal places to resolve to.

      Returns number

      The floored value of the number resolved to the specified decimal places.

    • Rounds a number to a specified number of decimal places.

      Parameters

      • num: number

        The number to round.

      • decimalPlaces: number = 0

        The number of decimal places to round to. Defaults to 0. Can be negative to round to tens, hundreds, etc.

      Returns number

      The rounded number.

    • Formats a number using fixed-point notation with accurate rounding.

      Parameters

      • num: number

        The number to format.

      • decimalPlaces: number

        The number of digits to appear after the decimal point.

      Returns string

      A string representation of the number in fixed-point notation.

    • Truncates a number to a specified number of decimal places, removing any fractional digits beyond that precision.

      Parameters

      • num: number

        The number to truncate.

      • decimalPlaces: number

        The number of decimal places to truncate to.

      Returns number

      The truncated number.