1.5.9 • Published 3 years ago

currency-formatter v1.5.9

Weekly downloads
58,639
License
MIT
Repository
github
Last release
3 years ago

Currency Formatter

Build Status

A simple Javascript utility that helps you to display currency properly

STOP! You probably don't need this library

TL;DR: This library was created a long time ago. You should use Internationalization API instead.

Please don't add another dependency which you don't need. All modern browsers (and node.js) have this functionality built-in and do a much better job at formatting currencies. e.g. #57

Example:

new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(100000000)
// => "$100,000,000.00"

new Intl.NumberFormat('en-US', { style: 'currency', currency: 'EUR' }).format(100000000)
// => "€100,000,000.00"

new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'USD' }).format(100000000)
// => "100.000.000,00 $"

new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(100000000)
// => "100.000.000,00 €"

new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR' }).format(100000000)
// => "100 000 000,00 €"

With that being said use this library if you need:

  • Your version of node.js doesn't come with the full-icu. See: #72 and #19214
  • Support old browsers.
  • Consistent formatting across all browsers.
  • You don't like the Intl APIs
  • ???

Install

npm install currency-formatter --save

Basic Usage

By specifying the currency code

var currencyFormatter = require('currency-formatter');

currencyFormatter.format(1000000, { code: 'USD' });
// => '$1,000,000.00'

currencyFormatter.format(1000000, { code: 'GBP' });
// => '£1,000,000.00'

currencyFormatter.format(1000000, { code: 'EUR' });
// => '1 000 000,00 €'

Or by specifying the locale

var currencyFormatter = require('currency-formatter');

currencyFormatter.format(1000000, { locale: 'en-US' });
// => '$1,000,000.00'

currencyFormatter.format(1000000, { locale: 'en-GB' });
// => '£1,000,000.00'

currencyFormatter.format(1000000, { locale: 'GB' });
// => '£1,000,000.00'

currencyFormatter.format(1000000, { locale: 'de-DE' });
// => '1.000.000,00 €'

currencyFormatter.format(1000000, { locale: 'nl-NL' });
// => '€1.000.000,00'

You can also get the currency information.

var currencyFormatter = require('currency-formatter');

currencyFormatter.findCurrency('USD');
// returns:
// {
//   code: 'USD',
//   symbol: '$',
//   thousandsSeparator: ',',
//   decimalSeparator: '.',
//   symbolOnLeft: true,
//   spaceBetweenAmountAndSymbol: false,
//   decimalDigits: 2
// }

Parse the number of a monetary value

currencyFormatter.unformat('$10.5', { code: 'USD' })
// => 10.5

currencyFormatter.unformat('$1,000,000', { code: 'USD' })
// => 1000000

currencyFormatter.unformat('10,5 €', { code: 'EUR' })
// => 10.5

currencyFormatter.unformat('1 000 000,00 €', { code: 'EUR' })
// => 1000000

currencyFormatter.unformat('1.000,99', { locale: 'de-DE' })
// => 1000.99

currencyFormatter.unformat('10\'000 CHF', { code: 'CHF' })
// => 10000

currencyFormatter.unformat('10.00 CHF', { code: 'CHF' })
// => 10

currencyFormatter.unformat('10,00 CHF', { code: 'CHF' })
// => 1000

Advanced Usage

Currency Formatter uses accounting library under the hood, and you can use its options to override the default behavior.

var currencyFormatter = require('currency-formatter');
currencyFormatter.format(1000000, {
  symbol: '@',
  decimal: '*',
  thousand: '^',
  precision: 1,
  format: '%v %s' // %s is the symbol and %v is the value
});

// => '1^000^000*0 @'

// Different formatting for positive and negative values
currencyFormatter.format(-10, {
  format: {
    pos: '%s%v' // %s is the symbol and %v is the value
    neg: '(%s%v)',
    zero: '%s%v'
  }
});

// => ($10)

You could also get a list of all the currencies here using one of the following:

var currencies = require('currency-formatter/currencies');
// OR
var currencyFormatter = require('currency-formatter');
var currencies = currencyFormatter.currencies;

Or the currencies in hashmap shape:

var currencies = require('currency-formatter/currencies.json');
// Result:
// {
//  "USD": {
//    "code": "USD",
//    "symbol": "$",
//    "thousandsSeparator": ",",
//    "decimalSeparator": ".",
//    "symbolOnLeft": true,
//    "spaceBetweenAmountAndSymbol": false,
//    "decimalDigits": 2
//  },
//  ...more currencies
// }

License

MIT

base-export@pronto-development/pronto-componentswiz-ng-libdirectpay-cardpayment@everreal/er-common-helpers@thesis-co/mechamittens-extensionpg-api-servergate-backend@crystaldesign/diva-storyant-nodejs-kit@infinitebrahmanuniverse/nolb-curlukes-redux-treekresus@everything-registry/sub-chunk-1426@bonanzainteractive/detectdevices_timer_tween7xui-prootp-react-reduxmystashpaklek-cliquinvoicenanoposreact-currency-number-formatreact-input-kitincarteraci-ppa-business-logicci-ppa-businss-logiccoinchartscoldsnap-utilitiesrevoicecrypto-price-monitorcreditstacks-dashboardcurrency.ordercurrency-krcents-to-currencycerebro-npm-testcalcloancomponents-baseultradatamanagertween_timer_coreurclisneaker_calculatorsvc-promodetectdevices1trailpack-proxy-cartstock-crypto-monitortinyformatteruno-serverless-handlebarsuno-serverless-nunjucksspidergraph@openreceipt/currency@nuskin/cart-sdk@ott_test/itinerary-body@edenjs/shop@fabrix/spool-cart@findify/ui-components@firestark/input@macellan/formatter@kenyip/react-native-currency-input@mechamittens/extension@mechamittens/uivue-invoice@cerezagroup/utilsutil01@ci24/ci-billing-sqlserver@ci24/ci-liquidate-service@sandboxcommerceeng/react@hello-charles/json-logic@conveyal/commute@dibs-tech/modelswallet-app@front10/car-dealership-book@finalytic/queue@finalytic/sdk@finalytic/utils@laverna_ritchie5/wallet@needish/qoopit-ui-components@jsweb001/company_profile_light_blue_themeemail-actionsemail-actions-v2er-common-componentseverest-native-shell@underdogio/pupadmin-dashboard@wbuilder/utilslc-commonlc-js-common@tillhub/vue-table-page@tillhub/datatableasksuite-corelokagen-v2luxury-escapesirie-utilsjackal-base-exportjackal-gamebase-export@wework/inventory-manager-components@wework/occupancy-map-componentsgamebase-exportfilebazaarfindify-ui-componentskitado-js
1.5.9

3 years ago

1.5.8

3 years ago

1.5.7

3 years ago

1.5.6

4 years ago

1.5.5

5 years ago

1.5.4

5 years ago

1.5.3

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.5

6 years ago

1.4.4

6 years ago

1.4.3

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

9 years ago

0.0.1

9 years ago