@wangcch/format-currency

1.0.1 • Public • Published

Format Currency

Help you with currency formatter

Build Status

Install

npm install --save @wangcch/format-currency

Usage

import { formatCurrency, parseCurrency } from "@wangcch/format-currency";
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

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

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

npm install && npm test

License

MIT

Package Sidebar

Install

npm i @wangcch/format-currency

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

11.2 kB

Total Files

9

Last publish

Collaborators

  • wangcch