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

# Number

> Metodi che jComponent aggiunge a Number.prototype per calcolare, arrotondare e formattare i numeri.

jComponent aggiunge questi metodi a `Number.prototype`. Scrivi un numero letterale tra parentesi per chiamarci un metodo, ad esempio `(5).add(3)`. I separatori e le valute usati qui provengono da [DEF](../def/), e i template accedono alla stessa formattazione tramite gli [helper di Tangular](../tangular-helpers/).

## API

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

Type: `method`

Aggiunge un numero o applica un'operazione scritta come testo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number or String` | yes |  | Il numero da sommare o l'operazione. |
| `decimals` | `Number` | no |  | Arrotonda per difetto un'operazione testuale a questo numero di decimali. |

Returns: `Number` Il risultato.

Un numero viene sommato al valore corrente. Un testo inizia con un operatore:

| Testo | Risultato per `200` |
| --- | --- |
| `'+10'` | `210` |
| `'-10'` | `190` |
| `'*10'` | `2000` |
| `'/10'` | `20` |
| `'+10%'` | `220` |
| `'-10%'` | `180` |
| `'10%'` | `20` |

`inc()` è un altro nome dello stesso metodo.

Example: Operazioni
```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`

Restituisce il valore assegnato all'intervallo che contiene il numero.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `condition` | `Object` | yes |  | Gli intervalli e i relativi valori. |
| `otherwise` | `Object` | no |  | Il valore restituito quando nessun intervallo corrisponde. |

Returns: `Object` Il valore dell'intervallo corrispondente, oppure `otherwise`.

Ogni chiave di `condition` è un intervallo: `'0-3'` include entrambi i limiti, `'7-'` significa 7 o più e `'-3'` significa 3 o meno. Gli intervalli vengono controllati nell'ordine delle chiavi e vince la prima corrispondenza. I limiti non possono essere numeri negativi.

Example: Scegli un'etichetta
```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`

Formatta il numero con un formatter registrato in `DEF.currencies`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `currency` | `String` | no |  | Il codice della valuta. |
| `a` | `Object` | no |  | Un argomento personalizzato per il formatter. |
| `b` | `Object` | no |  | Un argomento personalizzato per il formatter. |
| `c` | `Object` | no |  | Un argomento personalizzato per il formatter. |

Returns: `String` L'importo formattato.

Un formatter è una `function(value, a, b, c)` memorizzata sotto il proprio codice. Quando manca `currency`, viene usato `DEF.currency`. Un codice nella forma `[key]` viene letto da `ENV()`. Senza un formatter corrispondente, il numero viene scritto con due decimali.

Example: Registra una valuta
```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`

Arrotonda il numero per difetto a un certo numero di decimali.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number` | yes |  | Il numero di decimali. Usa `0` per un numero intero. |

Returns: `Number` Il numero arrotondato.

Example: Arrotonda per difetto
```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`

Scrive il numero con un separatore delle migliaia e un numero fisso di decimali.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number or String` | no |  | Il numero di decimali. |
| `separator` | `String` | no |  | Il separatore delle migliaia. |
| `decimalSeparator` | `String` | no |  | Il separatore decimale. |

Returns: `String` Il numero formattato.

I decimali in eccesso vengono troncati, non arrotondati; usa prima [round()](#round) quando l'arrotondamento è importante. Senza `decimals`, il numero mantiene i propri decimali. I separatori predefiniti sono `DEF.thousandsseparator` (uno spazio) e `DEF.decimalseparator` (un punto).

Quando il primo argomento è un testo, ha uno di due significati. Un testo nella forma `[key]` legge un valore esistente di `ENV()`, che è il numero di decimali o un oggetto con i campi `decimals`, `separator` e `decimalseparator`. Qualsiasi altro testo è il separatore delle migliaia e il secondo argomento è allora il numero di decimali.

Example: Decimali e separatori
```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: Impostazioni dall'ambiente
```js
ENV('price', { decimals: 1, separator: ',', decimalseparator: '.' });
(1234.56).format('[price]'); // "1,234.5"
```

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

Type: `method`

Scrive il numero come testo e lo riempie a sinistra.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `length` | `Number` | yes |  | La lunghezza finale. |
| `char` | `String` | no | `'0'` | Il carattere da aggiungere. |

Returns: `String` Il testo riempito.

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

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

Type: `method`

Scrive il numero come testo e lo riempie a destra.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `length` | `Number` | yes |  | La lunghezza finale. |
| `char` | `String` | no | `'0'` | Il carattere da aggiungere. |

Returns: `String` Il testo riempito.

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

### num.parseDate([offset])

Type: `method`

Crea un `Date` da un numero di millisecondi.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `offset` | `Number` | no | `0` | I millisecondi sommati al numero. |

Returns: `Date` La nuova data.

Example: Da un timestamp
```js
(1700000000000).parseDate().getTime(); // 1700000000000
(1700000000000).parseDate(1000).getTime(); // 1700000001000
```

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

Type: `method`

Sceglie un testo in base alla grandezza del numero.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `zero` | `String or Array` | yes |  | Il testo per 0, oppure tutte e quattro le forme. |
| `one` | `String` | yes |  | Il testo per 1. |
| `few` | `String` | yes |  | Il testo per i numeri maggiori di 1 e minori di 5. |
| `other` | `String` | yes |  | Il testo per tutti gli altri numeri. |

Returns: `String` Il testo scelto.

Il testo `zero` viene usato per 0, `one` per 1, `few` per i numeri maggiori di 1 e minori di 5, e `other` per tutti gli altri. Nel testo scelto, `#` scrive il numero e `##` lo scrive con [format()](#format).

Puoi passare le quattro forme anche come un unico array o come un unico testo separato da virgole. Un testo nella forma `[key]` viene letto da `ENV()`. Tutte e quattro le forme sono obbligatorie.

Example: Scegli una forma
```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`

Arrotonda il numero a un certo numero di decimali.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number` | no | `0` | Il numero di decimali. |

Returns: `Number` Il numero arrotondato.

Il numero di decimali non può essere negativo.

Example: Arrotonda un numero
```js
(1.005).round(2); // 1.01
(1.5).round(); // 2
(1.4).round(); // 1
```
