Number
Methods that jComponent adds to Number.prototype for formatting, rounding, calculating and pluralizing.
On this page
jComponent adds these methods to every number. The default separators and currencies are set in DEF, and the same formatting is available in templates through the Tangular helpers.
API
method
Number.add(value, [decimals])
Calculates a new number from an operation written as a string.
Number.add(value, [decimals])| Parameter | Type | Description |
|---|---|---|
value |
String or Number |
The operation, or a number to add. |
decimals? |
Number |
Cuts the result to this number of decimal places. |
Returns Number The result.
A number is added to the value. The string starts with the operator +, -, * or /. The forms +10% and -10% add or subtract a percentage of the number, and 10% returns that percentage. inc() is an alias of this method.
console.log((100).add('+10')); // 110
console.log((100).add(10)); // 110
console.log((100).add('*2')); // 200
console.log((100).add('/4')); // 25
console.log((100).add('-10%')); // 90
console.log((200).add('10%')); // 20
console.log((10).add('/3', 2)); // 3.33
console.log((100).inc('+5')); // 105
method
Number.currency([name], [a], [b], [c])
Formats the number with a currency registered in DEF.currencies.
Number.currency([name], [a], [b], [c])| Parameter | Type | Description |
|---|---|---|
name? |
String |
The currency name, or a [key] phrase that holds it. Default: DEF.currency |
a? |
Object |
Custom argument for the currency function. |
b? |
Object |
Custom argument for the currency function. |
c? |
Object |
Custom argument for the currency function. |
Returns String The formatted price.
A currency is a function that receives the number and the arguments a, b and c. A name that is not registered formats the number with two decimal places.
DEF.currencies.eur = value => value.format(2) + ' EUR';
DEF.currencies.usd = (value, suffix) => '$' + value.format(2) + suffix;
console.log((1234.5).currency('eur')); // 1 234.50 EUR
console.log((1234.5).currency('usd', ' net')); // $1 234.50 net
console.log((1234.5).currency('unknown')); // 1 234.50
method
Number.floor(decimals)
Rounds the number down to a number of decimal places.
Number.floor(decimals)| Parameter | Type | Description |
|---|---|---|
decimals |
Number |
The decimal places to keep. |
Returns Number The rounded number.
console.log((1.987).floor(2)); // 1.98
console.log((1.987).floor(0)); // 1
console.log((-1.981).floor(1)); // -2
method
Number.format([decimals], [separator], [decimalSeparator])
Writes the number with thousands and decimal separators.
Number.format([decimals], [separator], [decimalSeparator])| Parameter | Type | Description |
|---|---|---|
decimals? |
Number or String |
The decimal places, or a [key] phrase. |
separator? |
String |
The thousands separator. Default: DEF.thousandsseparator |
decimalSeparator? |
String |
The decimal separator. Default: DEF.decimalseparator |
Returns String The formatted number.
Extra decimal places are rounded. Missing decimal places are filled with zeros. A number in exponential notation is written in full. When decimals is omitted the number keeps its own decimal part.
The first argument can also be a [key] phrase for a value set with ENV(). The value is the number of decimals or an object with the fields decimals, separator and decimalseparator.
console.log((1234567.891).format()); // 1 234 567.891
console.log((1234.5).format(2)); // 1 234.50
console.log((1.999).format(2)); // 2.00
console.log((0.0000001).format()); // 0.0000001
console.log((1234.5).format(2, '.', ',')); // 1.234,50
ENV('money', { decimals: 2, separator: ',', decimalseparator: '.' });
console.log((1234.5).format('[money]')); // 1,234.50
method
Number.padLeft(length, [char])
Writes the number as text and fills its start up to a length.
Number.padLeft(length, [char])| Parameter | Type | Description |
|---|---|---|
length |
Number |
The final length. |
char? |
String |
The fill character. Default: 0 |
Returns String The padded text.
console.log((7).padLeft(3)); // 007
console.log((7).padLeft(3, '_')); // __7
method
Number.padRight(length, [char])
Writes the number as text and fills its end up to a length.
Number.padRight(length, [char])| Parameter | Type | Description |
|---|---|---|
length |
Number |
The final length. |
char? |
String |
The fill character. Default: 0 |
Returns String The padded text.
console.log((7).padRight(3)); // 700
console.log((7).padRight(3, '_')); // 7__
method
Number.parseDate([offset])
Converts a timestamp in milliseconds to a Date.
Number.parseDate([offset])| Parameter | Type | Description |
|---|---|---|
offset? |
Number |
Milliseconds added to the timestamp. Default: 0 |
Returns Date The date.
console.log((1700000000000).parseDate().toISOString()); // 2023-11-14T22:13:20.000Z
console.log((1700000000000).parseDate(3600000).toISOString()); // 2023-11-14T23:13:20.000Z
method
Number.pluralize(zero, one, few, other)
Selects the text that matches the number.
Number.pluralize(zero, one, few, other)| Parameter | Type | Description |
|---|---|---|
zero |
String or Array |
The text for 0, or all the forms. |
one? |
String |
The text for 1. |
few? |
String |
The text for numbers from 2 to 4. |
other? |
String |
The text for other numbers. |
Returns String The selected text.
zero is used for 0, one for 1, few for numbers greater than 1 and lower than 5, and other for the rest. In the selected text # writes the number and ## writes it formatted with format().
All four forms can also be passed as one array, as one comma-separated string, or as a [key] phrase that holds that string.
console.log((0).pluralize('no items', '# item', '# items', '## items')); // no items
console.log((1).pluralize('no items', '# item', '# items', '## items')); // 1 item
console.log((3).pluralize('no items', '# item', '# items', '## items')); // 3 items
console.log((1234).pluralize('no items', '# item', '# items', '## items')); // 1 234 items
console.log((7).pluralize('none,one,few,many')); // many
console.log((3).pluralize(['none', 'one', 'few', 'many'])); // few
method
Number.round([decimals])
Rounds the number to a number of decimal places.
Number.round([decimals])| Parameter | Type | Description |
|---|---|---|
decimals? |
Number |
The decimal places to keep. Default: 0 |
Returns Number The rounded number.
A negative value rounds to tens, hundreds and so on.
console.log((1.005).round(2)); // 1.01
console.log((1.5).round()); // 2
console.log((1234.5).round(-2)); // 1200