Function abbreviateNumber

  • Abbreviates a number to a more readable format using suffixes for thousands (K), millions (M), billions (B), and larger values. The function can handle both number and string inputs, and provides an option to format the suffix in lowercase or uppercase.

    Parameters

    • value: undefined | string | number

      The number or string to be abbreviated. If the value is undefined, the function returns an empty string. If the value cannot be converted to a number, the function returns 'Invalid input'.

    • Optional options: AbbreviateOptions

      An optional parameter to specify the formatting options for the suffix.

    Returns string

    The abbreviated number with an appropriate suffix or an error message if the input is invalid.

    Example

    // Basic usage with number input
    abbreviateNumber(123); // "123"
    abbreviateNumber(1234); // "1.2K"
    abbreviateNumber(1234567); // "1.2M"
    abbreviateNumber(1234567890); // "1.2B"

    // Basic usage with string input
    abbreviateNumber("1234567890123"); // "1.2T"

    // Using options to format the suffix in lowercase
    abbreviateNumber(1234567, { case: 'lower' }); // "1.2m"

    // Using options to format the suffix in uppercase
    abbreviateNumber(1234567, { case: 'upper' }); // "1.2M"

    // Handling undefined input
    abbreviateNumber(undefined); // ""

    // Handling invalid input
    abbreviateNumber("invalid input"); // "Invalid input"

Generated using TypeDoc