Single function that allows a number to be formatted to a currency with the sign of the user's local currency.
using npm:
$ npm install number-to-local-currency
using pnpm:
$ pnpm install number-to-local-currency
using CDN:
<script src="https://www.unpkg.com/[email protected]/dist/index.js"></script>
If you are not using CDN, you can import the library using import
or require
approach:
import { format } from "number-to-local-currency";
or
const format = require("number-to-local-currency");
Simple usage:
format(993.43); // when user is in USA - formatting number to US currency with US formatting output: $ 999.43
format(993.43); // when user is in Poland - formatting number to Polish currency with US formatting output: PLN 999.43
When changing default formatting type, all format types must be compatible with JavaScript's Intl:
format(993.43, "fr"); // formatting number to polish currency with french formatting output: 993,43 PLN
- Get user's timezone using moment-timezone guess function
- Get user's country code using countries-and-timezones
- Format number with JavaScript native Intl object
- Returns formatted number
value
– Number you want to formatnumberFormat
– Select format type compatible with JavaScript Intl - default value is en-USfallbackCountryCurrency
– In case something goes wrong and JavaScript isn't able to identify the user's country, you can set fallback country - default is US
This function works using the following three libraries: moment-timezone & country-to-currency & countries-and-timezones
MIT