1.11.0 • Published 12 months ago

@tuplo/numberfmt v1.11.0

Weekly downloads
30
License
MIT
Repository
github
Last release
12 months ago

Why

JS provides powerful number formatting with the standard built-in object Intl.NumberFormat, but we find its API a little verbose and hard to grasp its full potential. We took inspiration from older libraries like numbro.js and numeral.js and built a string based pattern for interacting with Intl.NumberFormat. Tiny footprint, no dependencies, works on the browser or nodejs.

Usage

import nf from '@tuplo/numberfmt';

// numeric
nf(123_456, '0,0.00'); // → 123,456.00

// currency
nf(123_456, '0,0GBP'); // → £123,456

// digital units
nf(123_456, '0b'); // → 120.56kb

// with locale
nf(123_456, '0,0.00', { locale: 'ar-EG' }); // → ١٢٣٬٤٥٦٫٧٩

// functional programming, partial application
const nfp = nf.partial('0,0.00');
nfp(123_456); // → 123,456.00

Install

$ npm install @tuplo/numberfmt

# or with yarn
$ yarn add @tuplo/numberfmt

Options

An optional set of options can be provided

nf(123_456, '0,0.00', { 
  locale: 'ar-EG',
  numberingSystem: 'arab'
}); // → ١٢٣٬٤٥٦٫٧٩

locale

string | optional | defaults to system locale

The BCP 47 language tag for the locale actually used.

numberingSystem

string | optional | derives from locale

Examples: arab, fullwide, hant, latn.

Reference

CharacterMeaning
0digit1
.decimal separator1.2
,thousands group separator1,234
[]optional
-negative sign-1
()negative sign (accounting)(123)
%percentage95%
oordinals1st
acompact notation (short display)1K
Acompact notation (long display)1 thousand
eexponential (scientific)1E4
Eexponential (engineering)100E3
bbits1.23Mb
Bbytes1.23TB
mlength1.23km
kmass1.23kg
USDcurrency symbolUS$1,000
scurrency narrow symbol$1,000
ccurrency codeUSD 1,000
ncurrency name1,000 US dollars

Numbers

ValueFormatResult
1234560,0123,456thousands separator
1234.50,0.01,234.5decimal separator
1234.50,0.001,234.50fixed number of decimal digits
1230.0123optional decimal separator
123.40.0123.4
123.40.00123.40
123.450.000123.45fixed and optional decimal digits
123.4560.000123.456
-1-0-1negative sign
1(0)1negative sign (accounting)
-1(0)(1)

Percentage

ValueFormatResult
10%100%
-0.120%-12%
0.123450.00%12.34%

Ordinals

ValueFormatResult
00o0th
10o1st
20o2nd
30o3rd
40o4th
12340,0o1,234th

Compact notation

ValueFormatResult
1230a123short display
12340a1K
123450a12K
1234560a123K
12345670a1M
12345678900a1B
12345678901230a1T
12340A1 thousandlong display
12345670A1 million
12345678900A1 billion
12345678901230A1 trillion
12345670.0a1.2Mcombined with numeric formats
12345670.0A1.2 million

Exponential

ValueFormatResult
10e1E0scientific (order-of-magnitude)
100e1E1
1000e1E2
10000e1E3
100000e1E4
10E1E0engineering (exponent of ten when divisible by three)
100E10E0
1000E100E0
10000E1E3
100000E10E3
1000000E100E3
123456780.0e1.2E7combined with numeric formats
123456780.0E12.3E6

Digital

ValueFormatResult
10b1bitbits (narrow)
10240b1kb
10485760b1Mb
10737418240b1Gb
10995116277760b1Tb
10B1Bbytes (narrow)
10240B1kB
10485760B1MB
10737418240B1GB
10995116277760B1TB
10 b1 bitbits (short)
10240 b1 kb
10 B1 bytebytes (short)
10240 B1 kB
15240b1.49kb2 fraction digits
15240B1.49kB

Length

ValueFormatResult
0.0010m1mmvalue in meters, formatted to closest unit (narrow)
0.010m1cm
10m1m
10000m1km
12000m1.2km
0.0010 m1 mm(short)
0.010 m1 cm
10 m1 m
10000 m1 km
12000 m1.2 km

Mass

ValueFormatResult
10k1kgnarrow
0.0010k1g
1.230k1.23kg
10 k1 kgshort
0.0010 k1 g
1.230 k1.23 kg

Currency

ValueFormatResult
123GBP£123symbol
12340,0GBP£1,234
12340,0.00GBP£1,234.00
123EUR€123
123JPYJP¥123
123USDUS$123
123CADCA$123
10000,0GBPs£1,000narrow symbol
10000,0EURs€1,000
10000,0USDs$1,000
10000,0CADs$1,000
10000,0JPYs¥1,000
10000,0GBPcGBP 1,000ISO currency code
10000,0EURcEUR 1,000
10000,0USDcUSD 1,000
10000,0JPYcJPY 1,000
10000,0GBPn1,000 British poundscurrency name
10000,0EURn1,000 euros
10000,0USDn1,000 US dollars
10000,0JPYn1,000 Japanese yen

License

MIT

1.11.0

12 months ago

1.9.5

1 year ago

1.10.0

1 year ago

1.9.4

1 year ago

1.9.3

2 years ago

1.9.1

2 years ago

1.8.2

2 years ago

1.9.0

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

1.9.2

2 years ago

1.7.4

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago