README
English | 简体中文
This is a lightweight JavaScript tool class for handling currency.
It aims to solve the problems of currency format and the most basic accuracy.
Features
- After compression, the file size is about 3KB
- Supports formatting numbers into strings in currency format
- Supports parsing strings in currency format into numbers
- Support chain operation
- Supports expanding instance methods and using plugins to enrich currency operations (such as mathematical operations)
I'm honored that it may be helpful to you. Please light up a star. Thank you!
Install
- npm
npm i -S light-currency
import Currency from 'light-currency'
// or
const Currency = require('light-currency')
- script
<script src="./light-currency.min.js"></script
Fast Start
The Currency class accepts Number or String data as constructor parameters.
p.s. The following cur
stands for an instance of Currency
new Currency(1000);// 1000
new Currency('1000');// 1000
Js limits the number of digits of Number type. Too many digits may cause precision loss, so * * String type value is recommended for large digits * *.
new Currency(123456789123456789);// 123456789123456780
new Currency("123456789123456789");// "123456789123456789"
Format operation(seeFormat):
new Currency(10000).format();// $10,000
new Currency(10000, {
prefix: '¥',
groupSize: 4,
}).format();// ¥1,0000
new Currency(10000).format({
prefix: '¥',
groupSize: 4,
});// ¥1,0000
Parsing operation(seeParse):
// Parsing ordinary format
new Currency('$1,000,000').value;// '1000000'
Currency.parse('$1,000,000.12').value;// '1000000.12'
// Parsing special format, need to pass in the config
Currency.parse('$1,000,000*12', {
decimalSeparator: '*',
}).value;// '1000000.12'
Mathematical operation(Need to load the plugin: light-currency-plugin-math ,see[Extension and plugin](###Extension and plugin)):
new Currency('1000').add('1000').sub(500).value;// 1500
Rounding operation(Need to load the plugin: light-currency-plugin-math,see[Extension and plugin](###Extension and plugin)):
new Currency('1000.456').toFixed(2).value;// 1000.46
new Currency('1000').toFixed(2).value;// 1000.00
new Currency('1000.456').toDP(2).value;// 1000.46
new Currency('1000').toDP(2).value;// 1000
Create Instance
Method
- Using constructor:
new Currency(value [, config])
- Using static method:
Currency.getInstance(value [, config])
- Using constructor:
Parameter
value:{Number|String},support Number type (1000), String type number ("1000"), String type number in common currency format ("$1000") .
config:{Object},It is used to set the formatting configuration of the instance. If it is not passed in, it defaults to static configuration,see[Format configuration](###Format configuration)
return
{Currency},Return the Currency instance
const cur1 = new Currency('123456789.123456789') const cur2 = Currency.getInstance('123456789.123456789')
Format
Method
cur.format([config])
Parameter
config:{Object},it is Instant format configuration, If it is not passed in, it defaults to the current instance configuration,see[Format configuration](###Format configuration)
return {String},returns a string in amount format
Example
const cur = new Currency('123456789.123456789') const foo = cur.format();// '$123,456,789.123456789' const bar = cur.format({ prefix: '¥', groupSize: 4 });// '¥1,2345,6789.123456789' cur.setConfig({ prefix: '€', groupSize: 3 }).format();// '€123,456,789.123456789'
Format Configuration
Config is a configuration item object for formatting,the item is:
- prefix
- decimalSeparator
- groupSeparator
- groupSize
- suffix
The light-currency provides three formatting configurations for more flexibility.
three formatting configurations:
- Static Configuration(Currency.config)
- Instance Configuration(cur.config)
- Instant Configuration(cur.format(config))
When executing the format method, config takes precedence in the following order: Instant Configuration > Instance Configuration > Static Configuration
Static Configuration:
Static configuration is stored under Currency.config.
The default value for static configuration is:
{
prefix: '