Number
Methods that jComponent adds to Number.prototype for calculating, rounding and formatting numbers.
On this page
jComponent adds these methods to Number.prototype. Write a number literal in parentheses to call a method on it, for example (5).add(3). The separators and currencies used here come from DEF, and templates reach the same formatting through the Tangular helpers.
API
method
num.add(value, [decimals])
Adds a number or applies an operation written as text.
num.add(value, [decimals])| Parameter | Type | Description |
|---|---|---|
value |
Number or String |
The number to add, or the operation. |
decimals? |
Number |
Rounds a text operation down to this number of decimals. |
Returns Number The result.
A number is added to the current value. A text starts with an operator:
| Text | Result for 200 |
|---|---|
'+10' | 210 |
'-10' | 190 |
'*10' | 2000 |
'/10' | 20 |
'+10%' | 220 |
'-10%' | 180 |
'10%' | 20 |
inc() is another name for the same method.
(5).add(3); // 8
(200).add('+10'); // 210
(200).add('*10'); // 2000
(200).add('+10%'); // 220
(200).add('-10%'); // 180
(200).add('10%'); // 20
(10).add('/3', 2); // 3.33
method
num.between(condition, [otherwise])
Returns the value assigned to the range that contains the number.
num.between(condition, [otherwise])| Parameter | Type | Description |
|---|---|---|
condition |
Object |
The ranges and their values. |
otherwise? |
Object |
The value returned when no range matches. |
Returns Object The value of the matching range, or otherwise.
Each key of condition is a range: '0-3' includes both limits, '7-' means 7 or more and '-3' means 3 or less. Ranges are checked in the order of the keys and the first match wins. The limits cannot be negative numbers.
var ranges = { '0-3': 'low', '4-6': 'medium', '7-': 'high' };
(5).between(ranges); // "medium"
(9).between(ranges); // "high"
(2).between({ '5-9': 'middle' }, 'other'); // "other"
method
num.currency([currency], [a], [b], [c])
Formats the number with a formatter registered in DEF.currencies.
num.currency([currency], [a], [b], [c])| Parameter | Type | Description |
|---|---|---|
currency? |
String |
The currency code. |
a? |
Object |
A custom argument for the formatter. |
b? |
Object |
A custom argument for the formatter. |
c? |
Object |
A custom argument for the formatter. |
Returns String The formatted amount.
A formatter is a function(value, a, b, c) stored under its code. When currency is missing, DEF.currency is used. A code in the form [key] is read from ENV(). Without a matching formatter, the number is written with two decimals.
DEF.currencies.eur = function(value) {
return value.format(2) + ' EUR';
};
(1234.5).currency('eur'); // "1 234.50 EUR"
(1234.5).currency('unknown'); // "1 234.50"
method
num.floor(decimals)
Rounds the number down to a number of decimals.
num.floor(decimals)| Parameter | Type | Description |
|---|---|---|
decimals |
Number |
The number of decimals. Use 0 for a whole number. |
Returns Number The rounded number.
(1.999).floor(2); // 1.99
(5.5).floor(0); // 5
(-1.555).floor(2); // -1.56
method
num.format([decimals], [separator], [decimalSeparator])
Writes the number with a thousands separator and a fixed number of decimals.
num.format([decimals], [separator], [decimalSeparator])| Parameter | Type | Description |
|---|---|---|
decimals? |
Number or String |
The number of decimals. |
separator? |
String |
The thousands separator. |
decimalSeparator? |
String |
The decimal separator. |
Returns String The formatted number.
Extra decimals are cut off, not rounded; use round() first when rounding matters. Without decimals, the number keeps its own decimals. The separators default to DEF.thousandsseparator (a space) and DEF.decimalseparator (a dot).
When the first argument is a text, it has one of two meanings. A text in the form [key] reads an existing ENV() value, which is the number of decimals or an object with the fields decimals, separator and decimalseparator. Any other text is the thousands separator, and the second argument is then the number of decimals.
(1234567.891).format(); // "1 234 567.891"
(1234.5).format(2); // "1 234.50"
(1.999).format(2); // "1.99"
(1234.5).format(0); // "1 234"
(1234.5).format(2, '.', ','); // "1.234,50"
(1234.5).format(','); // "1,234.5"
ENV('price', { decimals: 1, separator: ',', decimalseparator: '.' });
(1234.56).format('[price]'); // "1,234.5"
method
num.padLeft(length, [char])
Writes the number as text and pads it on the left side.
num.padLeft(length, [char])| Parameter | Type | Description |
|---|---|---|
length |
Number |
The final length. |
char? |
String |
The character to add. Default: '0' |
Returns String The padded text.
(5).padLeft(3); // "005"
(5).padLeft(3, ' '); // " 5"
method
num.padRight(length, [char])
Writes the number as text and pads it on the right side.
num.padRight(length, [char])| Parameter | Type | Description |
|---|---|---|
length |
Number |
The final length. |
char? |
String |
The character to add. Default: '0' |
Returns String The padded text.
(5).padRight(3); // "500"
(5).padRight(3, 'x'); // "5xx"
method
num.parseDate([offset])
Creates a Date from a number of milliseconds.
num.parseDate([offset])| Parameter | Type | Description |
|---|---|---|
offset? |
Number |
Milliseconds added to the number. Default: 0 |
Returns Date The new date.
(1700000000000).parseDate().getTime(); // 1700000000000
(1700000000000).parseDate(1000).getTime(); // 1700000001000
method
num.pluralize(zero, one, few, other)
Chooses a text by the size of the number.
num.pluralize(zero, one, few, other)| Parameter | Type | Description |
|---|---|---|
zero |
String or Array |
The text for 0, or all four forms. |
one |
String |
The text for 1. |
few |
String |
The text for numbers greater than 1 and lower than 5. |
other |
String |
The text for all other numbers. |
Returns String The chosen text.
The 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 chosen text, # writes the number and ## writes it with format().
The four forms can also be passed as one array or as one text separated by commas. A text in the form [key] is read from ENV(). All four forms are required.
(0).pluralize('no items', '# item', '# items', '# items'); // "no items"
(1).pluralize('no items', '# item', '# items', '# items'); // "1 item"
(3).pluralize('no items', '# item', '# items', '# items'); // "3 items"
(5000).pluralize('none', 'one', 'few', '## items'); // "5 000 items"
(2).pluralize('none,one,few,many'); // "few"
(7).pluralize(['none', 'one', 'few', 'many']); // "many"
method
num.round([decimals])
Rounds the number to a number of decimals.
num.round([decimals])| Parameter | Type | Description |
|---|---|---|
decimals? |
Number |
The number of decimals. Default: 0 |
Returns Number The rounded number.
The number of decimals cannot be negative.
(1.005).round(2); // 1.01
(1.5).round(); // 2
(1.4).round(); // 1