0.0.6 • Published 1 year ago
number-to-local-currency v0.0.6
Format number to local currency
Single function enabling you to format number to currency with user's local currency sign.
Installing
using npm:
$ npm install number-to-local-currency
using pnpm:
$ pnpm install number-to-local-currency
using CDN
<script src="https://www.unpkg.com/number-to-local-currency@0.0.1/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");
Examples
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 formats type must be compatible with javascript's Intl
format(993.43, "fr"); // formatting number to polish currency with french formatting output: 993,43 PLN
How it works
- 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
Function params
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
Credits
This function work using the following three libraries: moment-timezone & country-to-currency & countries-and-timezones
License
MIT