https://github.com/wangcch/format-currency
help you with currency formatter
https://github.com/wangcch/format-currency
Last synced: 11 months ago
JSON representation
help you with currency formatter
- Host: GitHub
- URL: https://github.com/wangcch/format-currency
- Owner: wangcch
- License: mit
- Created: 2018-10-30T17:01:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T15:18:01.000Z (over 3 years ago)
- Last Synced: 2025-05-17T11:07:02.729Z (about 1 year ago)
- Language: JavaScript
- Size: 467 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Format Currency
Help you with currency formatter
[](https://travis-ci.org/wangcch/format-currency)
[](https://www.npmjs.com/package/@wangcch/format-currency)
[](./LICENSE)
## Install
```shell
npm install --save @wangcch/format-currency
```
## Usage
```js
import { formatCurrency, parseCurrency } from "@wangcch/format-currency";
```
```js
formatCurrency(value, [options]);
parseCurrency(value);
```
### Options
| parameter | type | default | explain |
| ------------ | ------- | ------- | ---------------------- |
| decimalDigit | number | 2 | Reserved decimal digit |
| segmentDigit | number | 3 | Currency segment digit |
| isSegment | boolean | true | Character segmentation |
## Demo
### formatCurrency
```js
formatCurrency("123"); // "123.00"
formatCurrency("-123"); //"-123.00"
formatCurrency(123); // "123.00"
formatCurrency("123456"); // "123,456.00"
formatCurrency("1234.567"); // "1234.57"
formatCurrency("123.00", { decimalDigit: 0 }); // "123"
formatCurrency("123456", { segmentDigit: 5 }); // "1,23456.00"
formatCurrency("123456", { isSegment: false }); // "123456.00"
formatCurrency("1234.5678", { decimalDigit: 1, isSegment: false }); // "1234.6"
// Error
formatCurrency("abc"); // NaN
formatCurrency("1."); // NaN
formatCurrency(".1"); // NaN
formatCurrency("1.2.3"); // NaN
formatCurrency(Infinity); //NaN
formatCurrency(null); //NaN
formatCurrency(undefined); //NaN
```
### parseCurrency
```js
parseCurrency("123,456"); //123456
parseCurrency("123.456"); //123.456
// Error
parseCurrency("abc"); // NaN
parseCurrency("1."); // NaN
parseCurrency(".1"); // NaN
parseCurrency("1.2.3"); // NaN
parseCurrency(Infinity); //NaN
parseCurrency(null); //NaN
parseCurrency(undefined); //NaN
```
## Test
```js
npm install && npm test
```
## License
MIT