Format
How to format numbers, currencies, etc, and unformat them.
Format
Numbers can be formatted to look like currency, percentages, times, or even plain old numbers with decimal places, thousands, and abbreviations.
Since version 2.0.0, formatting is done via a literal object allowing easier customization and more predictable output.
All available options can be found in the code directly.
Numbers
Number | Format | String |
---|---|---|
10000 | ||
10000.23 | ||
10000.23 | ||
-10000 | ||
10000.1234 | ||
10000.1234 | ||
1.23 | ||
1.234 | ||
1.2345 | ||
1.23456 | ||
-10000 | ||
-0.23 | ||
-0.23 | ||
0.23 | ||
1230974 | ||
1460 | ||
-104000 | ||
233434 | ||
233000 | ||
1 | ||
52 | ||
23 | ||
100 |
Average
numbro provides an easy mechanism to round up any number with the key average
.
Note that the abbreviation is language-specific.
Number | Format | String |
---|---|---|
123 | ||
1234 | ||
12345 | ||
123456 | ||
1234567 | ||
12345678 | ||
123456789 |
In addition to this, when numbers are rounded up, one can specify the precision wanted with the key totalLength
.
Number | Format | String |
---|---|---|
1234567891 | ||
1234567891 | ||
1234567891 | ||
1234567891 | ||
1234567891 | ||
1234567891 | ||
1234567891 | ||
1234567891 | ||
1234567891 | ||
1234567891 | ||
1234567891 |
Currency
numbro supports cultural currency formatting via the function formatCurrency
.
Number | Language | String |
---|---|---|
1000.234 | en-US | |
1000.234 | fr-FR | |
1000.234 | ja-JP | |
1000.234 | ru-RU |
You can of course precise a more specific format, as long as you do not use the $
(that would override defaults).
Number | Language | String |
---|---|---|
1000.234 | en-US | |
1000.234 | fr-FR | |
1000.234 | ja-JP | |
1000.234 | ru-RU |
But if you want to, you can always precise a format by hand to override the languages defaults.
Number | Format | String |
---|---|---|
1000.234 | ||
1000.2 | ||
1001 | ||
-1000.234 | ||
-1000.234 | ||
1230974 |
Bytes
Number | Format | String |
---|---|---|
100 | ||
2048 | ||
7884486213 | ||
3467479682787 |
Percentages
Number | Format | String |
---|---|---|
1 | ||
0.974878234 | ||
-0.43 | ||
0.43 |
Time
Number | Format | String |
---|---|---|
25 | ||
238 | ||
63846 |
If you want to format time more than that, we recommend you Momentjs which inspired numbro.
Unformat
Got a formatted string? Use the unformat
function to make it useful again.
String | Function | Number |
---|---|---|
'10,000.123' | .unformat('10,000.123') | |
'0.12345' | .unformat('0.12345') | |
'1.23m' | .unformat('1.23m') | |
'23rd' | .unformat('23rd') | |
'$10,000.00' | .unformat('$10,000.00') | |
'100B' | .unformat('100B') | |
'3.154TB' | .unformat('3.154TB') | |
'-76%' | .unformat('-76%') | |
'2:23:57' | .unformat('2:23:57') |
Defaults
numbro allows you to the default format to use, as well as how to format 0.
Default format
Set a default format so you can use .format()
without a string. The default format to '0,0'
.
Default zero
Set a custom output when formatting numbers with a value of 0
.