Created
August 26, 2022 14:04
-
-
Save bhaireshm/1e9909b3b9ce0025a7256db34176c860 to your computer and use it in GitHub Desktop.
Converts number into formatted currency value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts number into formatted currency value. | |
* | |
* @param {Number} val - Integer value | |
* @param {Intl.NumberFormatOptions} options - An object with some or all of the properties of Intl.NumberFormatOptions | |
* @returns formatted value. | |
* | |
* @example | |
* console.log(currencyFormatter(1234567890.1997)); // ₹1,23,45,67,890.20 | |
* console.log(currencyFormatter(1234567890, {locales: "en-US", currency: "USD", maximumFractionDigits: 0})); // $1,234,567,890 | |
* | |
* @link Reference - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options | |
*/ | |
function currencyFormatter(val, options = {}) { | |
options = { | |
currency: "INR", | |
style: "currency", | |
maximumFractionDigits: 2, | |
locales: options?.locales || "en-IN", | |
...options, | |
} | |
return new Intl.NumberFormat(options.locales, options).format(val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment