Function formatNumber

  • Formats a number with Bangla digits and adds commas for thousands, lakhs, crores, etc.

    Parameters

    • num: string | number

      The number or string to format.

    • strict: boolean = true

      If true, throws an error for invalid input.

    • inEnglish: boolean = false

      If true, returns the result in English digits.

    Returns string

    The formatted string with Bangla digits and commas.

    If the input is null, undefined, or exceeds the maximum safe integer value.

    formatNumber(12345); // "১২,৩৪৫"
    formatNumber('-১২৩৪৫'); // "-১২,৩৪৫"
    formatNumber(' -১২৩৪৫ '); // "-১২,৩৪৫" (automatically trims the input)
    formatNumber('১২৩৪৫', true, true); // "12,345" (if inEnglish is true, returns the result in English digits)
    formatNumber(1234567890123456); // "১,২৩,৪৫,৬৭,৮৯,০১,২৩,৪৫৬"
    formatNumber("১২৩৪৫৬৭৮৯০১২৩৪৫৬"); // "১,২৩,৪৫,৬৭,৮৯,০১,২৩,৪৫৬"
    formatNumber("১,২১৩,৪৫১,২৩৪,৫১২,৩৪৫"); // "১,২১,৩৪,৫১,২৩,৪৫,১২,৩৪৫"
    formatNumber("abc"); // Throws an error (if strict is true)
    formatNumber("abc", false); // '' (if strict is false, returns an empty string)