Function toDigits

  • Round a number to n digits of precision after the decimal point.

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    • value: number

      Value to format

    • precision: number

      Number of digits of precision after the decimal point

    Returns number

    Number rounded to the specified number of digits

    Example

    const value = 12.345;

    toDigits(value, 2); // 12.35