All Projects → scurker → Currency.js

scurker / Currency.js

Licence: mit
A javascript library for handling currencies

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Currency.js

Money
A money and currency library for PHP
Stars: ✭ 855 (-61.38%)
Mutual labels:  currency, money
Ngx Currency
📦 Currency mask module for Angular
Stars: ✭ 161 (-92.73%)
Mutual labels:  currency, money
Android Money
Simple money and currency converter library for android.
Stars: ✭ 66 (-97.02%)
Mutual labels:  currency, money
Dinero.js
Create, calculate, and format money in JavaScript and TypeScript.
Stars: ✭ 5,286 (+138.75%)
Mutual labels:  currency, money
Frankfurter
💱 Currency data API
Stars: ✭ 123 (-94.44%)
Mutual labels:  currency, money
Money
A precise, type-safe representation of a monetary amount in a given currency
Stars: ✭ 817 (-63.1%)
Mutual labels:  currency, money
Currencyconverter
Utilities for doing currency conversion with the Money library
Stars: ✭ 78 (-96.48%)
Mutual labels:  currency, money
Jsr354 Api
JSR 354 - Money and Currency API
Stars: ✭ 262 (-88.17%)
Mutual labels:  currency, money
Javamoney Lib
JavaMoney financial libraries, extending and complementing JSR 354
Stars: ✭ 104 (-95.3%)
Mutual labels:  currency, money
Money Open Exchange Rates
A gem that calculates the exchange rate using published rates from open-exchange-rates. Compatible with the money gem.
Stars: ✭ 87 (-96.07%)
Mutual labels:  currency, money
Money
PHP implementation of Fowler's Money pattern.
Stars: ✭ 3,868 (+74.71%)
Mutual labels:  currency, money
Django Prices
Django fields for the prices module
Stars: ✭ 135 (-93.9%)
Mutual labels:  currency, money
Vue Numeric
Input field component to display a formatted currency value based on Vue.js
Stars: ✭ 341 (-84.6%)
Mutual labels:  currency, money
Go Money
Go implementation of Fowler's Money pattern
Stars: ✭ 887 (-59.94%)
Mutual labels:  currency, money
Cashify
💸 Lightweight currency conversion library, successor of money.js
Stars: ✭ 329 (-85.14%)
Mutual labels:  currency, money
Swap
💱 Currency exchange rates library
Stars: ✭ 1,195 (-46.03%)
Mutual labels:  currency, money
react-local-currency
💵 💴Shows the price of your services in the customer's currency 💶 💷
Stars: ✭ 21 (-99.05%)
Mutual labels:  money, currency
Laravel Money
Currency formatting and conversion package for Laravel
Stars: ✭ 261 (-88.21%)
Mutual labels:  currency, money
Countries
Countries - ISO 3166 (ISO3166-1, ISO3166, Digit, Alpha-2 and Alpha-3) countries codes and names (on eng and rus), ISO 4217 currency designators, ITU-T E.164 IDD calling phone codes, countries capitals, UN M.49 regions codes, ccTLD countries domains, IOC/NOC and FIFA letters codes, VERY FAST, NO maps[], NO slices[], NO init() funcs, NO external links/files/data, NO interface{}, NO specific dependencies, Databases/JSON/GOB/XML/CSV compatible, Emoji countries flags and currencies support, full support ISO-3166-1, ISO-4217, ITU-T E.164, Unicode CLDR and ccTLD standarts.
Stars: ✭ 85 (-96.16%)
Mutual labels:  currency, money
Exchanger
🏢 Currency exchange rates framework for PHP
Stars: ✭ 133 (-93.99%)
Mutual labels:  currency, money

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.

Features

  • 0 dependencies!
  • Immutable
  • Flexible formatting options
  • Handles any type of currency input, strings, numbers, or another currency instance

Installation

With npm:

npm install --save currency.js

With yarn:

yarn add currency.js

Via cdn:

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

Need the latest cutting edge? All commits on master are tagged with next on npm:

npm install --save currency.js@next

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

Currency accepts decimal values (i.e. 1.23) with a default precision of 2, but can accept a minor currency unit (e.g. cents in a dollar). This will respect the precision option when parsing.

currency(123, { fromCents: true });               // 1.23
currency('123', { fromCents: true });             // 1.23
currency(123, { fromCents: true, precision: 0 }); // 123
currency(123, { fromCents: true, precision: 3 }); // 0.123

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, { symbol: "€", 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: $
Currency symbol included 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.

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.

format default null Allows you to customize the format of the currency when calling currency.format(). format passes in the currency object as well as the options object to the function and expects a string to be returned. Use this when the provided formatting options do not meet your needs.

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

fromCents default: false
Parse the amount value as a minor currency unit (e.g. cents in a dollar) instead of dollars.

View more examples and full documentation at https://currency.js.org.

Internationalization Examples

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

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(); // "$1,234.56"
JPY(1234.56).format(); // "¥1,235"
GAS(1234.56).format(); // "$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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].