• Converts a number to a string with custom formatting based on its value.

    Parameters

    • n: number

      The number to convert to a string.

    Returns string

    • The formatted string representation of the number, or "0" for non-finite values (e.g., NaN, Infinity).

    numberToString

    Formats numbers as follows:

    • Non-finite numbers (NaN, Infinity) return "0".
    • 0.0 returns "0".
    • Numbers < 0.0001 or > 10000 use exponential notation with 2 decimal places.
    • Integers (except 0) append ".0".
    • Numbers with more than 2 decimal places are rounded to 2 decimal places.
    • Other numbers are converted to their default string representation.
    console.log(numberToString(0.0));      // Outputs: "0"
    console.log(numberToString(0.00005)); // Outputs: "5.00e-5"
    console.log(numberToString(15000)); // Outputs: "1.50e+4"
    console.log(numberToString(42)); // Outputs: "42.0"
    console.log(numberToString(3.14159)); // Outputs: "3.14"
    console.log(numberToString(1.23)); // Outputs: "1.23"
    console.log(numberToString(NaN)); // Outputs: "0"
    console.log(numberToString(Infinity)); // Outputs: "0"