---
title: Number
project: jComponent V19
version: 19.194
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v19/19-194/reference/number/
updated: 2026-11-18T13:58:29.465Z
---

# Number

> Methods that jComponent adds to Number.prototype for calculating, rounding and formatting numbers.

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](../def/), and templates reach the same formatting through the [Tangular helpers](../tangular-helpers/).

## API

### num.add(value, [decimals])

Type: `method`

Adds a number or applies an operation written as text.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number or String` | yes |  | The number to add, or the operation. |
| `decimals` | `Number` | no |  | 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.

Example: Operations
```js
(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
```

### num.between(condition, [otherwise])

Type: `method`

Returns the value assigned to the range that contains the number.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `condition` | `Object` | yes |  | The ranges and their values. |
| `otherwise` | `Object` | no |  | 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.

Example: Pick a label
```js
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"
```

### num.currency([currency], [a], [b], [c])

Type: `method`

Formats the number with a formatter registered in `DEF.currencies`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `currency` | `String` | no |  | The currency code. |
| `a` | `Object` | no |  | A custom argument for the formatter. |
| `b` | `Object` | no |  | A custom argument for the formatter. |
| `c` | `Object` | no |  | 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.

Example: Register a currency
```js
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"
```

### num.floor(decimals)

Type: `method`

Rounds the number down to a number of decimals.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number` | yes |  | The number of decimals. Use `0` for a whole number. |

Returns: `Number` The rounded number.

Example: Round down
```js
(1.999).floor(2); // 1.99
(5.5).floor(0); // 5
(-1.555).floor(2); // -1.56
```

### num.format([decimals], [separator], [decimalSeparator])

Type: `method`

Writes the number with a thousands separator and a fixed number of decimals.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number or String` | no |  | The number of decimals. |
| `separator` | `String` | no |  | The thousands separator. |
| `decimalSeparator` | `String` | no |  | The decimal separator. |

Returns: `String` The formatted number.

Extra decimals are cut off, not rounded; use [round()](#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.

Example: Decimals and separators
```js
(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"
```

Example: Settings from the environment
```js
ENV('price', { decimals: 1, separator: ',', decimalseparator: '.' });
(1234.56).format('[price]'); // "1,234.5"
```

### num.padLeft(length, [char])

Type: `method`

Writes the number as text and pads it on the left side.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `length` | `Number` | yes |  | The final length. |
| `char` | `String` | no | `'0'` | The character to add. |

Returns: `String` The padded text.

Example: Leading zeros
```js
(5).padLeft(3); // "005"
(5).padLeft(3, ' '); // "  5"
```

### num.padRight(length, [char])

Type: `method`

Writes the number as text and pads it on the right side.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `length` | `Number` | yes |  | The final length. |
| `char` | `String` | no | `'0'` | The character to add. |

Returns: `String` The padded text.

Example: Trailing characters
```js
(5).padRight(3); // "500"
(5).padRight(3, 'x'); // "5xx"
```

### num.parseDate([offset])

Type: `method`

Creates a `Date` from a number of milliseconds.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `offset` | `Number` | no | `0` | Milliseconds added to the number. |

Returns: `Date` The new date.

Example: From a timestamp
```js
(1700000000000).parseDate().getTime(); // 1700000000000
(1700000000000).parseDate(1000).getTime(); // 1700000001000
```

### num.pluralize(zero, one, few, other)

Type: `method`

Chooses a text by the size of the number.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `zero` | `String or Array` | yes |  | The text for 0, or all four forms. |
| `one` | `String` | yes |  | The text for 1. |
| `few` | `String` | yes |  | The text for numbers greater than 1 and lower than 5. |
| `other` | `String` | yes |  | 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()](#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.

Example: Choose a form
```js
(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"
```

### num.round([decimals])

Type: `method`

Rounds the number to a number of decimals.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number` | no | `0` | The number of decimals. |

Returns: `Number` The rounded number.

The number of decimals cannot be negative.

Example: Round a number
```js
(1.005).round(2); // 1.01
(1.5).round(); // 2
(1.4).round(); // 1
```
