Journalism Format - v1.1.12
    Preparing search index...

    Function formatNumber

    • Formats a number according to specified style, rounding, and display options. This versatile function can handle various numerical formatting needs.

      The function supports two main styles: "cbc" (Canadian Broadcasting Corporation style, typically English) and "rc" (Radio-Canada style, typically French), which dictate the thousands separator and decimal marker.

      Parameters

      • number: number

        The number to be formatted.

      • options: {
            abbreviation?: boolean;
            decimals?: number;
            fixed?: boolean;
            nearestInteger?: number;
            position?: boolean;
            prefix?: string;
            round?: boolean;
            sign?: boolean;
            significantDigits?: number;
            style?: "cbc" | "rc";
            suffix?: string;
        } = {}

        An object containing various formatting options.

        • Optionalabbreviation?: boolean

          If true, the number will be abbreviated (e.g., 1,200,000 becomes "1.2M"). Defaults to false.

        • Optionaldecimals?: number

          The number of decimal places to round to.

        • Optionalfixed?: boolean

          If true, the number will be displayed with a fixed number of decimal places as specified by decimals, padding with zeros if necessary. Defaults to false.

        • OptionalnearestInteger?: number

          The base to which the number should be rounded (e.g., 10 for rounding to the nearest ten, 100 for nearest hundred).

        • Optionalposition?: boolean

          If true, formats the number as an ordinal position (e.g., "1st", "2nd" in English, "1er", "2e" in French).

        • Optionalprefix?: string

          A string to prepend before the formatted number.

        • Optionalround?: boolean

          If true, the number will be rounded to the nearest integer or based on decimals, significantDigits, or nearestInteger options. Defaults to false.

        • Optionalsign?: boolean

          If true, a "+" sign will be prepended to positive numbers. Negative numbers always retain their "-" sign. Defaults to false.

        • OptionalsignificantDigits?: number

          The number of significant digits to round to.

        • Optionalstyle?: "cbc" | "rc"

          The formatting style to apply. Can be "cbc" (default, typically English with comma thousands separator and dot decimal) or "rc" (typically French with space thousands separator and comma decimal).

        • Optionalsuffix?: string

          A string to append after the formatted number.

      Returns string

      The formatted number as a string.

      // Basic usage: Format a number with thousands separator.
      const num1 = formatNumber(1234567.89);
      console.log(num1); // "1,234,567.89"
      // With sign and rounding to 0 decimals.
      const num2 = formatNumber(1234.567, { sign: true, decimals: 0 });
      console.log(num2); // "+1,235"
      // French style with abbreviation.
      const num3 = formatNumber(1234567, { style: "rc", abbreviation: true });
      console.log(num3); // "1,2 M"
      // Fixed number of decimals with prefix and suffix.
      const num4 = formatNumber(98.765, { decimals: 2, fixed: true, prefix: "$", suffix: " CAD" });
      console.log(num4); // "$98.77 CAD"
      // Formatting as an ordinal position.
      const position1 = formatNumber(1, { position: true });
      console.log(position1); // "1st"

      const position2 = formatNumber(2, { position: true, style: "rc" });
      console.log(position2); // "2e"