currency.js vs intl vs accounting-js vs currency-formatter vs numeral
货币格式化
currency.jsintlaccounting-jscurrency-formatternumeral类似的npm包:

货币格式化

货币格式化库是 JavaScript 中用于将数字转换为货币格式的工具。这些库提供了格式化、解析和操作货币值的功能,考虑了不同的货币符号、千位分隔符和小数位。它们在电子商务、金融应用和任何需要以货币形式显示数字的地方都非常有用。accounting-js 是一个轻量级的库,提供简单的货币格式化和数学运算。currency-formatter 专注于根据区域设置格式化货币,支持多种货币和自定义格式。currency.js 提供了一个面向对象的方法来处理货币值,支持链式调用和精确的数学运算。intl 是一个内置的国际化 API,提供强大的格式化功能,包括货币、数字和日期。numeral 是一个灵活的库,支持数字格式化、解析和自定义格式,适用于需要复杂数字表示的应用。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
currency.js808,8623,382-615 年前MIT
intl740,2911,691-7910 年前MIT
accounting-js011746.9 kB121 年前MIT
currency-formatter0212-155 年前MIT
numeral09,704-3459 年前MIT

功能对比: currency.js vs intl vs accounting-js vs currency-formatter vs numeral

货币格式化

  • currency.js:

    currency.js 提供精确的货币格式化,支持链式调用和自定义格式。它允许开发者定义货币符号和格式,提供更大的灵活性。

  • intl:

    intlIntl.NumberFormat 接口提供强大的货币格式化功能,支持根据区域自动格式化。它是一个内置的解决方案,遵循国际化标准,适合需要高质量格式化的应用。

  • accounting-js:

    accounting-js 提供基本的货币格式化功能,支持自定义货币符号、千位分隔符和小数位。它适合简单的货币显示,但不支持根据区域自动格式化。

  • currency-formatter:

    currency-formatter 专注于根据区域设置格式化货币,支持多种货币和自定义格式。它可以根据提供的区域和货币代码自动调整格式,非常适合国际化应用。

  • numeral:

    numeral 提供灵活的数字格式化功能,支持自定义货币格式。虽然它不是专门针对货币设计的,但可以通过自定义格式实现复杂的货币显示。

区域支持

  • currency.js:

    currency.js 允许开发者自定义货币符号和格式,但不提供内置的区域支持。需要手动配置以适应不同区域。

  • intl:

    intlIntl.NumberFormat 接口提供全面的区域支持,能够根据区域自动格式化货币、数字和日期。它是一个标准化的解决方案,支持多种语言和区域。

  • accounting-js:

    accounting-js 支持基本的区域设置,但需要手动配置货币符号和格式。它不提供自动区域识别功能。

  • currency-formatter:

    currency-formatter 提供强大的区域支持,能够根据区域代码自动格式化货币。它内置了多种货币和格式,适合国际化应用。

  • numeral:

    numeral 支持自定义区域设置,但需要手动配置。它不提供内置的区域支持,适合需要灵活配置的应用。

数学运算

  • currency.js:

    currency.js 提供精确的货币运算,支持链式调用和自定义运算。它是处理货币计算的强大工具,适合需要高精度运算的应用。

  • intl:

    intl 不提供数学运算功能,主要用于格式化和国际化。它适合需要格式化但不涉及计算的场景。

  • accounting-js:

    accounting-js 提供基本的数学运算功能,如加、减、乘、除,支持货币值的精确计算。它适合简单的财务计算,但功能较为有限。

  • currency-formatter:

    currency-formatter 主要专注于货币格式化,不提供数学运算功能。它适合需要格式化但不需要计算的场景。

  • numeral:

    numeral 提供基本的数字运算功能,支持加、减、乘、除,但不专门针对货币。它适合需要对数字进行简单运算的应用。

代码示例

  • currency.js:

    currency.js 货币格式化和运算示例

    import { Currency } from 'currency.js';
    
    // 创建货币实例
    const price = Currency(1234.56, { symbol: '$' });
    const tax = Currency(0.1); // 10% 税
    
    // 计算总价
    const total = price.add(price.multiply(tax));
    console.log(total.format()); // 输出: $1,357.12
    
  • intl:

    intl 货币格式化示例

    const numberFormat = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
    const formatted = numberFormat.format(1234567.89);
    console.log(formatted); // 输出: $1,234,567.89
    
  • accounting-js:

    accounting-js 货币格式化示例

    import accounting from 'accounting-js';
    
    // 格式化货币
    const formatted = accounting.formatMoney(1234567.89, { symbol: '$' });
    console.log(formatted); // 输出: $1,234,567.89
    
    // 货币运算
    const sum = accounting.add(100, 200);
    console.log(sum); // 输出: 300
    
  • currency-formatter:

    currency-formatter 货币格式化示例

    import { format } from 'currency-formatter';
    
    // 根据区域格式化货币
    const formatted = format(1234567.89, { code: 'USD' });
    console.log(formatted); // 输出: $1,234,567.89
    
  • numeral:

    numeral 数字格式化示例

    import numeral from 'numeral';
    
    // 格式化数字
    const formatted = numeral(1234567.89).format('$0,0.00');
    console.log(formatted); // 输出: $1,234,567.89
    
    // 解析数字
    const value = numeral('$1,234,567.89').value();
    console.log(value); // 输出: 1234567.89
    

如何选择: currency.js vs intl vs accounting-js vs currency-formatter vs numeral

  • currency.js:

    选择 currency.js 如果您需要一个面向对象的解决方案来处理货币值,支持链式调用和精确运算。它适合需要对货币进行更复杂操作的项目。

  • intl:

    选择 intl 如果您需要一个内置的解决方案,支持多种国际化功能,包括货币格式化。它适合需要遵循现代 Web 标准的应用。

  • accounting-js:

    选择 accounting-js 如果您需要一个轻量级的库来进行基本的货币格式化和简单的数学运算。它特别适合不需要复杂功能的项目。

  • currency-formatter:

    选择 currency-formatter 如果您需要根据不同区域设置格式化货币。它支持多种货币和自定义格式,适合需要国际化支持的应用。

  • numeral:

    选择 numeral 如果您需要一个灵活的库来处理数字格式化和解析,支持自定义格式。它适合需要对数字进行复杂格式化的应用。

currency.js的README

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