Numeral
Numeral is a standard Deno module for formatting and manipulating numbers.
π§ How to use
import { numeral } from 'https://deno.land/x/numeral@v0.1.0/mod.ts';
π‘ Usage
π Create
Create an instance of a numeral. Numeral takes numbers or strings that it trys to convert into a number.
const myNumeral = numeral(1000);
const value = myNumeral.value();
// 1000
const myNumeral2 = numeral('1,000');
const value2 = myNumeral2.value();
// 1000
Input | Value |
---|---|
numeral(974) | 974 |
numeral(0.12345) | 0.12345 |
numeral(β10,000.12β) | 10000.12 |
numeral(β23rdβ) | 23 |
numeral(β$10,000.00β) | 10000 |
numeral(β100Bβ) | 100 |
numeral(β3.467TBβ) | 3467000000000 |
numeral(β-76%β) | -0.76 |
numeral(β2:23:57β) | NaN |
π Format
Numbers can be formatted to look like currency, percentages, times, or even plain old numbers with decimal places, thousands, and abbreviations.
const string = numeral(1000).format('0,0');
// '1,000'
Numbers
Number | Format | String |
---|---|---|
10000 | β0,0.0000β | 10,000.0000 |
10000.23 | β0,0β | 10,000 |
10000.23 | β+0,0β | +10,000 |
-10000 | β0,0.0β | -10,000.0 |
10000.1234 | β0.000β | 10000.123 |
100.1234 | β00000β | 00100 |
1000.1234 | β000000,0β | 001,000 |
10 | β000.00β | 010.00 |
10000.1234 | β0[.]00000β | 10000.12340 |
-10000 | β(0,0.0000)β | (10,000.0000) |
-0.23 | β.00β | -.23 |
-0.23 | β(.00)β | (.23) |
0.23 | β0.00000β | 0.23000 |
0.23 | β0.0[0000]β | 0.23 |
1230974 | β0.0aβ | 1.2m |
1460 | β0 aβ | 1 k |
-104000 | β0aβ | -104k |
1 | β0oβ | 1st |
100 | β0oβ | 100th |
Currency
Number | Format | String |
---|---|---|
1000.234 | β$0,0.00β | $1,000.23 |
1000.2 | β0,0[.]00 $β | 1,000.20 $ |
1001 | β$ 0,0[.]00β | $ 1,001 |
-1000.234 | β($0,0)β | ($1,000) |
-1000.234 | β$0.00β | -$1000.23 |
1230974 | β($ 0.00 a)β | $ 1.23 m |
Bytes
Number | Format | String |
---|---|---|
100 | β0bβ | 100B |
1024 | β0bβ | 1KB |
2048 | β0 ibβ | 2 KiB |
3072 | β0.0 bβ | 3.1 KB |
7884486213 | β0.00bβ | 7.88GB |
3467479682787 | β0.000 ibβ | 3.154 TiB |
Percentages
Number | Format | String |
---|---|---|
1 | β0%β | 100% |
0.974878234 | β0.000%β | 97.488% |
-0.43 | β0 %β | -43 % |
0.43 | β(0.000 %)β | 43.000 % |
Time
Number | Format | String |
---|---|---|
25 | β00:00:00β | 0:00:25 |
238 | β00:00:00β | 0:03:58 |
63846 | β00:00:00β | 17:44:06 |
Exponential
Number | Format | String |
---|---|---|
1123456789 | β0,0e+0β | 1e+9 |
12398734.202 | β0.00e+0β | 1.24e+7 |
0.000123987 | β0.000e+0β | 1.240e-4 |
π Functions
Value
The value is always available.
const number = numeral(1000);
const string = number.format('0,0');
// '1,000'
const value = number.value();
// 1000
Manipulate
Not that you will use these often, but theyβre there when you need them.
const number = numeral(1000);
const added = number.add(10);
// 1010
Before | Function | After |
---|---|---|
1000 | .add(100) | 1100 |
1100 | .subtract(100) | 1000 |
1000 | .multiply(100) | 100000 |
100000 | .divide(100) | 1000 |
Set
Set the value of your numeral object.
const number = numeral();
number.set(1000);
const value = number.value();
// 1000
Difference
Find the difference between your numeral object and a value
const number = numeral(1000),
value = 100;
const difference = number.difference(value);
// 900
Clone
Go ahead and clone any numeral object while youβre at it.
const a = numeral(1000);
const b = numeral(a);
const c = a.clone();
const aVal = a.set(2000).value();
// 2000
const bVal = b.value();
// 1000
const cVal = c.add(10).value();
// 1010
π Settings
Default Formatting
Set a default format so you can use .format() without a string. The default format to β0,0β
const number = numeral(1000);
number.format();
// '1,000'
numeral.defaultFormat('$0,0.00');
number.format();
// '$1,000.00'
Custom Zero and Null Formatting
Set a custom output when formatting numerals with a value of 0 or null
const number = numeral(0);
const nullNumber = numeral(null);
numeral.zeroFormat('N/A');
numeral.nullFormat('N/A');
const zero = number.format('0.0')
// 'N/A'
const na = nullNumber.format('0.0')
// 'N/A'
β Issues
If you think any of the Numeral
can be improved, please do open a PR with any updates and submit any issues. Also, I will continue to improve this, so you might want to watch/star this repository to revisit.
πͺ Contribution
Weβd love to have your helping hand on contributions to Numeral
by forking and sending a pull request!
Your contributions are heartily β‘ welcome, recognized and appreciated. (βΏβ βΏβ )
How to contribute:
- Open pull request with improvements
- Discuss ideas in issues
- Spread the word
- Reach out with any feedback