number-to-local-currency
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

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

  1. Get user's timezone using moment-timezone guess function
  2. Get user's country code using countries-and-timezones
  3. Format number with javascript native Intl object
  4. Returns formatted number

Function params

  • value - Number you want to format
  • numberFormat - Select format type compatible with javascript Intl - default value is en-US
  • fallbackCountryCurrency - 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

Package Sidebar

Install

npm i number-to-local-currency

Weekly Downloads

1

Version

0.0.6

License

MIT

Unpacked Size

27.3 kB

Total Files

9

Last publish

Collaborators

  • pawel1894