---
title: Number
project: jComponent V20
version: 20.015
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v20/20-015/reference/number/
updated: 2026-11-18T13:58:19.462Z
---

# Number

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

jComponent adds these methods to every number. The default separators and currencies are set in [DEF](../def/), and the same formatting is available in templates through the [Tangular helpers](../tangular-helpers/).

## API

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

Type: `method`

Calculates a new number from an operation written as a string.

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

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

### Number.currency([name], [a], [b], [c])

Type: `method`

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

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | no | `DEF.currency` | The currency name, or a `[key]` phrase that holds it. |
| `a` | `Object` | no |  | Custom argument for the currency function. |
| `b` | `Object` | no |  | Custom argument for the currency function. |
| `c` | `Object` | no |  | 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.

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

### Number.floor(decimals)

Type: `method`

Rounds the number down to a number of decimal places.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number` | yes |  | The decimal places to keep. |

Returns: `Number` The rounded number.

Example: Rounding down
```js
console.log((1.987).floor(2)); // 1.98
console.log((1.987).floor(0)); // 1
console.log((-1.981).floor(1)); // -2
```

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

Type: `method`

Writes the number with thousands and decimal separators.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number or String` | no |  | The decimal places, or a `[key]` phrase. |
| `separator` | `String` | no | `DEF.thousandsseparator` | The thousands separator. |
| `decimalSeparator` | `String` | no | `DEF.decimalseparator` | The decimal separator. |

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()](../globals/). The value is the number of decimals or an object with the fields `decimals`, `separator` and `decimalseparator`.

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

Example: Environment formats
```js
ENV('money', { decimals: 2, separator: ',', decimalseparator: '.' });
console.log((1234.5).format('[money]')); // 1,234.50
```

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

Type: `method`

Writes the number as text and fills its start up to a length.

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

Returns: `String` The padded text.

Example: Padding
```js
console.log((7).padLeft(3)); // 007
console.log((7).padLeft(3, '_')); // __7
```

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

Type: `method`

Writes the number as text and fills its end up to a length.

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

Returns: `String` The padded text.

Example: Padding
```js
console.log((7).padRight(3)); // 700
console.log((7).padRight(3, '_')); // 7__
```

### Number.parseDate([offset])

Type: `method`

Converts a timestamp in milliseconds to a `Date`.

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

Returns: `Date` The date.

Example: Timestamps
```js
console.log((1700000000000).parseDate().toISOString()); // 2023-11-14T22:13:20.000Z
console.log((1700000000000).parseDate(3600000).toISOString()); // 2023-11-14T23:13:20.000Z
```

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

Type: `method`

Selects the text that matches the number.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `zero` | `String or Array` | yes |  | The text for 0, or all the forms. |
| `one` | `String` | no |  | The text for 1. |
| `few` | `String` | no |  | The text for numbers from 2 to 4. |
| `other` | `String` | no |  | 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.

Example: Four forms
```js
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
```

Example: One value with all the forms
```js
console.log((7).pluralize('none,one,few,many')); // many
console.log((3).pluralize(['none', 'one', 'few', 'many'])); // few
```

### Number.round([decimals])

Type: `method`

Rounds the number to a number of decimal places.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number` | no | `0` | The decimal places to keep. |

Returns: `Number` The rounded number.

A negative value rounds to tens, hundreds and so on.

Example: Rounding
```js
console.log((1.005).round(2)); // 1.01
console.log((1.5).round()); // 2
console.log((1234.5).round(-2)); // 1200
```
