2.0.4 • Published 7 days ago

currency.js v2.0.4

Weekly downloads
85,372
License
MIT
Repository
github
Last release
7 days ago

currency.js logo

currency.js

Build Status Coverage Status npm gzip size

currency.js is a lightweight ~1kb javascript library for working with currency values. It was built to work around floating point issues in javascript. This talk by Bartek Szopka explains in detail why javascript has floating point issues.

currency.js works with values as integers behind the scenes, resolving some of the most basic precision problems.

2.51 + .01;                   // 2.5199999999999996
currency(2.51).add(.01);      // 2.52

2.52 - .01;                   // 2.5100000000000002
currency(2.52).subtract(.01); // 2.51

This should work for most reasonable values of currencies. As long as your currency values are less than 253 (in cents) or 90,071,992,547,409.91 you should be okay.

Installation

With npm:

npm install --save currency.js

With yarn:

yarn add currency.js

Via cdn:

<script src="https://unpkg.com/currency.js@~1.1.0/dist/currency.min.js"></script>

Usage

Currency will accept numbers, strings, or the currency object itself as values.

currency(123);      // 123.00
currency(1.23);     // 1.23
currency("1.23")    // 1.23
currency("$12.30")  // 12.30

var value = currency("123.45");
currency(value);    // 123.45

There's various arithmetic methods that help take the guesswork out of trying to resolve floating point problems.

currency(123.50).add(0.23);       // 123.73
currency(5.00).subtract(0.50);    // 4.50
currency(45.25).multiply(3);      // 135.75
currency(1.12).distribute(5);     // [0.23, 0.23, 0.22, 0.22, 0.22]

There's even a built in formatter that will automatically place comma delimiters in the right place.

currency("2,573,693.75").add("100,275.50").format();  // "2,673,969.25"
currency("1,237.72").subtract(300).format();          // "937.72"

You can also change the format, localizing the decimal and/or delimiter to your locale.

var euro = value => currency(value, { separator: ".", decimal: "," });
euro("2.573.693,75").add("100.275,50").format();  // "2.673.969,25"
euro("1.237,72").subtract(300).format();          // "937,72"

Options

currency.js comes with its own set of default options conforming to USD. You can customize these according to your locale.

symbol default: $ When formatWithSymbol is set to true, will include the currency symbol when calling currency.format().

separator default: , Separator dividing the number groups when calling currency.format().

decimal default: . Decimal used when calling currency.format().

precision default: 2 Number of decimal places to store as cents.

formatWithSymbol default: false Includes the symbol option when calling currency.format().

pattern default: !# Allows you to customize the format pattern using ! as replacement for the currency symbol and # as replacement for the currency amount.

negativePattern default: -!# Allows you to customize the negative format pattern using ! as replacement for the currency symbol and # as replacement for the currency amount.

errorOnInvalid default: false If an invalid value such as null or undefined is passed in, will throw an error.

increment default: null When implementing a currency that implements rounding, setting the increment value will allow you to set the closest increment to round the display value to. currency(1.48, { increment: .05 }); // => 1.50

useVedic default: false Formats number groupings using the Indian Numbering System, i.e. 10,00,000.00

View more examples and full documentation at scurker.github.io/currency.js.

v1.0.0 breaking changes

In all version prior to v1.0.0, currency options were global. These global options were removed in v1.0.0 and now are passed to each instance of currency.js as the second param. This allows you to set options without any unintended side effects.

v0.4.x

currency.settings.separator = " ";
currency.settings.decimal = ",";
currency.settings.symbol = "€";
currency.settings.formatWithSymbol = true;

v1.x

currency(1.23, { separator: " ", decimal: ",", symbol: "€", formatWithSymbol: true })

If you need to work with multiple currency values, the easiest way is to setup factory functions with your required currency settings.

const USD = value => currency(value, { symbol: "$", precision: 2 });
const JPY = value => currency(value, { symbol: "¥", precision: 0 });
const GAS = value => currency(value, { precision: 3 });

USD(1234.56).format(true); // "$1,234.56"
JPY(1234.56).format(true); // "¥1,235"
GAS(1234.56).format(true); // "$1,234.560"

Add-ons

Other Libraries

Maybe currency.js isn't the right fit for your needs. Check out some of these other fine libraries:

License

MIT

@uponcommerce/api-plugin-paymentshub-dls-react@shipengine/ipaas@shipengine/integration-platform-sdkconvert-dollars.js@wikala/supermarketsui-reactjscorbeappcorbeta@cats-cradle/validation-decorators@infinitebrahmanuniverse/nolb-cur@everything-registry/sub-chunk-1426dorix_common@o1/validextract-product-from-metafn-dsfn-dsmfundle-credit-libraryfundle-currency-libraryel-input-currencyel-form-plusetpp-uievery-utilsipsky-utilshpa-ui-kitgrossbooks-print-templatesgridsome-plugin-jammery@ettawallet/react-corehalcon-tools@finalytic/queue@finalytic/sdk@finalytic/utilsgjs-template-filler@dynamic-framework/ui@dynamic-framework/ui-react@formml/clientkapix-graphqljvss-utils-functionskowalskikoreaats-lib-io-v1koreaats-lib-io-v1-teststubborn-yupownit-api-commonmui-advanced-tablepaygate-sdknumber-to-croctopus-design-systemoptum-qme-component-libraryorange-knows-packageplum-apilmy-utilsma-ui-alayers-design-systemmad-formattermaria-utilsmelck-componentesmedfro-print-templatesmvr-servicesnewhis-my-moduleray-templateprioricomponentsprixa-telemedicine-rn-sdktcomponentrettsnkstubber-form-fields-pkgundeterminisportlink-ui-kitsoderiz-etp_uisofa-logicsr-dashboard-cardsstackbee-currencystackbee-io-currencycrypto-index-fundcsv-sorttutorstack-frontend-logiccurrency-el-inputvendure-plugin-picqervh-checkoututils-ui-lib-pocv-currutils77di-fulfillmentdgykits@shopware-pwa/theme-base@vsi-components/react-components@vendure/payments-plugin@vendure.ecommerce/payments-plugin@sprinque/checkout@quantix-ict/debtt@rama-developer/common-components@uxf/localize@uxf/numbers@solarains/z-components@qinwm1/utils@sujian/utils@reactify-apps/searchyx-ui@wholezb/filters@wikala/spmkt@windingtree/wt-pricing-algorithms@windingtree/wt-booking-api
2.0.4-09d991e8

7 days ago

2.0.4-9f026f0a

3 months ago

2.0.4-6168e612

8 months ago

2.0.4-a6d038fc

5 months ago

2.0.4-87546c75

6 months ago

2.0.4-9f8f3994

10 months ago

2.0.4-6738131a

12 months ago

2.0.4-5055b39e

1 year ago

2.0.4-292a7058

1 year ago

2.0.4-768454a1

1 year ago

2.0.4-602edadc

1 year ago

2.0.4-d5060300

1 year ago

2.0.4-77fbfc6c

1 year ago

2.0.4-9172787b

1 year ago

2.0.4-edb9f084

1 year ago

2.0.4-177f71df

1 year ago

2.0.4-44c2d73a

1 year ago

2.0.4-fdd73875

2 years ago

2.0.4-8a05b81b

2 years ago

2.0.4-00bc4a32

2 years ago

2.0.4-1641f943

2 years ago

2.0.4-0187a2cb

2 years ago

2.0.4-74116c5a

2 years ago

2.0.4-b3a78ab0

2 years ago

2.0.4-b90e918a

2 years ago

2.0.4-369fb4c1

2 years ago

2.0.4-852ec9cc

2 years ago

2.0.4-7dbb6560

2 years ago

2.0.4-8637016

2 years ago

2.0.4-84456f89

2 years ago

2.0.4-e66bc5d0

2 years ago

2.0.4-7204f260

2 years ago

2.0.4-aa3a2ba5

2 years ago

2.0.4-1fd9252e

2 years ago

2.0.4-61acd106

2 years ago

2.0.4-4459dc2c

2 years ago

2.0.4-dbb2b4ff

2 years ago

2.0.4-98104df6

2 years ago

2.0.4-50943e4d

2 years ago

2.0.4-3dd3c3e5

2 years ago

2.0.4-43b8afb6

2 years ago

2.0.4-caad648f

2 years ago

2.0.4-52fddd60

2 years ago

2.0.4-f44fa6ec

2 years ago

2.0.4-1d96ec5f

2 years ago

2.0.4-36e9f0c9

2 years ago

2.0.4-1a96ea7e

2 years ago

2.0.4-293e282e

2 years ago

2.0.4-68e210f8

2 years ago

2.0.4-57ae075f

2 years ago

2.0.4-a796688f

2 years ago

2.0.4-d8992f4b

2 years ago

2.0.4-ea556eae

2 years ago

2.0.4-27b270e9

2 years ago

2.0.4-19926218

2 years ago

2.0.4-5417c509

2 years ago

2.0.4-9f23457f

3 years ago

2.0.4-81f54b37

3 years ago

2.0.4-7cffae5f

3 years ago

2.0.4-4d5f7bfe

3 years ago

2.0.4-56b772e7

3 years ago

2.0.4-98158da5

3 years ago

2.0.4-36bbc955

3 years ago

2.0.4-a6d296f5

3 years ago

2.0.4-4acc0d23

3 years ago

2.0.4-7a9d56e7

3 years ago

2.0.4-45644097

3 years ago

2.0.4-1e0d5593

3 years ago

2.0.4

3 years ago

2.0.3-83da51b1

3 years ago

2.0.4-444163bd

3 years ago

2.0.4-781f73cb

3 years ago

2.0.3-66276c87

3 years ago

2.0.3-65e9b29f

3 years ago

2.0.3-97001c05

3 years ago

2.0.3-678789f3

3 years ago

2.0.3-8c317288

3 years ago

2.0.3-ba57bfa9

3 years ago

2.0.3-e298871b

3 years ago

2.0.3-4a7946a2

3 years ago

2.0.3-66b7a0c6

3 years ago

2.0.3-0e48bd48

3 years ago

2.0.3-a1b16099

3 years ago

2.0.3-e1558668

3 years ago

2.0.3-83f9a835

3 years ago

2.0.3-4611c1cc

3 years ago

2.0.3-67f50d82

3 years ago

2.0.3-958f5c78

3 years ago

2.0.3-7047aae1

4 years ago

2.0.3-3d18eb14

4 years ago

2.0.3-317f1987

4 years ago

2.0.3-0b680505

4 years ago

2.0.2-f2dd2213

4 years ago

2.0.3

4 years ago

2.0.2-2f44a464

4 years ago

2.0.2-205ac48e

4 years ago

2.0.2-95fefd68

4 years ago

2.0.2-7d929d99

4 years ago

2.0.2-83f95acd

4 years ago

2.0.2-2c98725b

4 years ago

2.0.2

4 years ago

2.0.1-a3c2b324

4 years ago

2.0.1

4 years ago

2.0.0-c5952f0c

4 years ago

2.0.1-44b3be4c

4 years ago

2.0.0-508fba48

4 years ago

2.0.0

4 years ago

1.2.2-bc03eaf9

4 years ago

1.2.2-cc4aa875

4 years ago

1.2.2-a54d3c68

4 years ago

1.2.2-92e704d3

4 years ago

1.2.2-0f95ab05

4 years ago

1.2.2-cf95bc7c

4 years ago

1.2.2-40e700fe

4 years ago

1.2.2-7defc4bf

4 years ago

1.2.2-de058516

4 years ago

1.2.2-ed2e31a3

4 years ago

1.2.2-b59ca9c9

4 years ago

1.2.2-f7ec5522

5 years ago

1.2.2-ac059316

5 years ago

1.2.2-2c90b4ea

5 years ago

1.2.2-10bfb20b

5 years ago

1.2.2-2db57d5e

5 years ago

1.2.2-19005a5d

5 years ago

1.2.2-ec8ab613

5 years ago

1.2.2-e7e55ce7

5 years ago

1.2.2-107ef98e

5 years ago

1.2.2-1f6094aa

5 years ago

1.2.2-03b5b376

5 years ago

1.2.2-a8ba23cd

5 years ago

1.2.2-04030b4a

5 years ago

1.2.2-ad0fb3a3

5 years ago

1.2.2-1b9eeb75

5 years ago

1.2.2-4ac25608

5 years ago

1.2.2-fdf68eec

5 years ago

1.2.2

5 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.2.0-beta.1

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.1.0-beta.3

6 years ago

1.1.0-beta.2

6 years ago

1.1.0-beta.1

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5-test.1

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

7 years ago

1.0.0-rc1

7 years ago

0.4.4

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.4

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.1

9 years ago

0.2.0

10 years ago