3.0.1 • Published 1 year ago

decimal-format v3.0.1

Weekly downloads
4,583
License
ISC
Repository
github
Last release
1 year ago

DecimalFormat

format decimal numbers with a specific pattern

Install

$ npm install decimal-format --save

or use yarn

$ yarn add decimal-format --save

Usage

import DecimalFormat from 'decimal-format';

const df = new DecimalFormat('#,##0.0#');
df.format(1234.995);  // 1,235.0
df.format(1234.956);  // 1,234.96

const df1 = new DecimalFormat('#,##0.0#%');
df1.format(0.34995); // 35.0%

const df2 = new DecimalFormat('#,##0.0#‰');
df2.format(0.034993); // 34.99‰

const df3 = new DecimalFormat('¥#,##0.00元');
df3.format(1234.995); // ¥1,235.00元

RoundingMode defines the rounding modes of the formatting

import DecimalFormat, { RoundingMode } from 'decimal-format';

const df = new DecimalFormat('#,##0.0#');
df.setRoundingMode(RoundingMode.UP);    // round away from zero
df.format(2.1)      // 3.0
df.format(-2.1)     // -3.0

df.setRoundingMode(RoundingMode.UNNECESSARY) // assert that no rounding is necessary
df.format(2.11)     // 2.11
df.format(2.123)    // throw exception

RoundingMode

EnumDescription
UPround away from zero
DOWNround towards zero
CEILINGround towards positive infinity
FLOORround towards negative infinity
HALF_UPround towards "nearest neighbor" unless both neighbors are equidistant, in which case round up
HALF_DOWNround towards "nearest neighbor" unless both neighbors are equidistant, in which case round down
HALF_EVENround towards the "nearest neighbor" unless both neighbors are equidistant, in which case round toward the even neighbor
UNNECESSARYassert that the requested operation has an exact result, hence no rounding is necessary
3.0.1

1 year ago

3.0.0

2 years ago

2.0.2

3 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.2.0

5 years ago

1.1.0

6 years ago

1.0.0

6 years ago