currency.js

repository·main·Indexed 25 days ago

https://github.com/scurker/currency.js

A lightweight (~1kb) JavaScript library for working with currency values. It provides tools for currency arithmetic and formatting while avoiding common floating-point precision errors by working with integers internally. Features include support for custom symbols, separators, precision, minor unit parsing (fromCents), and the Indian Numbering System (Vedic).

Tokens
4K
Snippets
33
Records
36
Agent score
86%

What's inside currency.js

  1. Format currencies using different locales and symbols

    main

    By default, currency.js uses a US locale. Because currency instances are immutable, you can create specialized factory functions to handle different international formats by passing an options object to the currency() constructor.

    Common options for formatting include:

    • precision: Number of decimal places.
    • symbol: The currency symbol to display.
    • decimal: The decimal separator.
    • separator: The thousands separator.
    const USD = value => currency(value);
    const JPY = value => currency(value, { precision: 0, symbol: '¥' });
    const EURO = value => currency(value, { symbol: '€', decimal: ',', separator: '.' });
    
    USD(1234.567).format(); // => "$1,234.57"
    JPY(1234.567).format(); // => "¥1,235"
    EURO(1234.567).format(); // => "€1.234,57"
  2. Use babel-plugin-transform-currency-operators for currency syntax

    main
    You can use the experimental babel-plugin-transform-currency-operators plugin to transform standard mathematical operators into currency.js method calls. This allows you to write code like currency(1.23) + 4.56 and have it automatically transformed into currency(1.23).add(4.56) during the Babel compilation process.
  3. Format currency for UI display

    main

    Use the .format() method to return a formatted string with thousands separators. The library also supports negative values represented by a leading minus sign or parentheses.

    var c = currency("$1,234.56").add("890.12"); // 2124.68
    c.format(); // 2,124.68
    
    // Negative values
    currency("-$5,000").add(1234.56);  // -3765.44
    currency("($5,000)").add(1234.56); // -3765.44
  4. Initialize currency values with various input types

    main

    You can initialize a currency object using strings, numbers, decimals, or another currency object. The library automatically handles parsing, including stripping currency symbols and formatting characters like commas.

    // Numbers
    currency(1); // => "1.00"
    currency(123); // => "123.00"
    
    // Decimals
    currency(1.00); // => "1.00"
    currency(1.23); // => "1.23"
    
    // Strings
    currency("1.23"); // => "1.23"
    currency("$12.30"); // => "12.30"
    currency("£1,234,567.89"); // => "1,234,567.89"
    
    // Currency objects
    let c1 = currency(1.23);
    let c2 = currency(4.56);
    currency(7.89).add(c1).add(c2); // => "13.68"
  5. Perform arithmetic operations to avoid floating point errors

    main
    Use .add() and .subtract() methods instead of standard JavaScript arithmetic operators (+, -) to avoid IEEE 754 floating point precision issues. The library works with integers internally to ensure accuracy.
  6. Configure the grouping separator

    main

    The separator option defines the character used between number groupings when calling .format(). The default value is ",".

    currency(1234.56, { separator: ',' }).format(); // => "1,234.56"
    currency(1234.56, { separator: ' ' }).format(); // => "1 234.56"
  7. Parse values from minor units (cents)

    main

    By default, currency accepts decimal values (e.g., 1.23). If you need to parse values representing minor currency units (e.g., 123 representing $1.23), set fromCents: true. This option respects the precision setting.

    currency(123456, { fromCents: true });               // => "1234.56"
    currency('123456', { fromCents: true });             // => "1234.56"
    currency(123456, { fromCents: true, precision: 0 }); // => "123456"
    currency(123456, { fromCents: true, precision: 3 }); // => "123.456"
  8. Set a rounding increment

    main

    The increment option allows you to specify the closest increment to round the display value to. This is useful for currencies that implement specific rounding rules.

    var currencyRounding = value => currency(value, { increment: .05 });
    currencyRounding(1.09); // => { intValue: 109, value: 1.09 }
    currencyRounding(1.09).format(); // => "1.10"
    currencyRounding(1.06); // => { intValue: 106, value: 1.06 }
    currencyRounding(1.06).format(); // => "1.05"