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

# Number

> I metodi che jComponent aggiunge a Number.prototype per formattare, arrotondare, calcolare e gestire i plurali.

jComponent aggiunge questi metodi a ogni numero. I separatori e le valute predefiniti si impostano in [DEF](../def/), e la stessa formattazione è disponibile nei template tramite gli [helper di Tangular](../tangular-helpers/).

## API

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

Type: `method`

Calcola un nuovo numero a partire da un'operazione scritta come stringa.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String or Number` | yes |  | L'operazione, oppure un numero da sommare. |
| `decimals` | `Number` | no |  | Tronca il risultato a questo numero di cifre decimali. |

Returns: `Number` Il risultato.

Un numero viene sommato al valore. La stringa inizia con l'operatore `+`, `-`, `*` o `/`. Le forme `+10%` e `-10%` aggiungono o sottraggono una percentuale del numero, mentre `10%` restituisce quella percentuale. `inc()` è un alias di questo metodo.

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

Formatta il numero con una valuta registrata in `DEF.currencies`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | no | `DEF.currency` | Il nome della valuta, oppure un'espressione `[key]` che lo contiene. |
| `a` | `Object` | no |  | Un argomento personalizzato per la funzione della valuta. |
| `b` | `Object` | no |  | Un argomento personalizzato per la funzione della valuta. |
| `c` | `Object` | no |  | Un argomento personalizzato per la funzione della valuta. |

Returns: `String` Il prezzo formattato.

Una valuta è una funzione che riceve il numero e gli argomenti `a`, `b` e `c`. Un nome non registrato formatta il numero con due cifre decimali.

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

Arrotonda il numero per difetto a un determinato numero di cifre decimali.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number` | yes |  | Le cifre decimali da mantenere. |

Returns: `Number` Il numero arrotondato.

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

Scrive il numero con i separatori delle migliaia e dei decimali.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number or String` | no |  | Le cifre decimali, oppure un'espressione `[key]`. |
| `separator` | `String` | no | `DEF.thousandsseparator` | Il separatore delle migliaia. |
| `decimalSeparator` | `String` | no | `DEF.decimalseparator` | Il separatore decimale. |

Returns: `String` Il numero formattato.

Le cifre decimali in eccesso vengono arrotondate. Quelle mancanti vengono riempite con zeri. Un numero in notazione esponenziale viene scritto per intero. Quando `decimals` è omesso, il numero mantiene la propria parte decimale.

Il primo argomento può essere anche un'espressione `[key]` per un valore impostato con [ENV()](../globals/). Il valore è il numero di decimali oppure un oggetto con i campi `decimals`, `separator` e `decimalseparator`.

Example: Formattazione
```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: Formati dall'ambiente
```js
ENV('money', { decimals: 2, separator: ',', decimalseparator: '.' });
console.log((1234.5).format('[money]')); // 1,234.50
```

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

Type: `method`

Scrive il numero come testo e ne riempie l'inizio fino a una determinata lunghezza.

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

Returns: `String` Il testo riempito.

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

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

Type: `method`

Scrive il numero come testo e ne riempie la fine fino a una determinata lunghezza.

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

Returns: `String` Il testo riempito.

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

### Number.parseDate([offset])

Type: `method`

Converte un timestamp in millisecondi in un `Date`.

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

Returns: `Date` La data.

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

Seleziona il testo che corrisponde al numero.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `zero` | `String or Array` | yes |  | Il testo per 0, oppure tutte le forme. |
| `one` | `String` | no |  | Il testo per 1. |
| `few` | `String` | no |  | Il testo per i numeri da 2 a 4. |
| `other` | `String` | no |  | Il testo per gli altri numeri. |

Returns: `String` Il testo selezionato.

`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 selezionato `#` scrive il numero e `##` lo scrive formattato con `format()`.

Tutte e quattro le forme possono essere passate anche come un unico array, come un'unica stringa separata da virgole o come un'espressione `[key]` che contiene quella stringa.

Example: Quattro forme
```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: Un unico valore con tutte le forme
```js
console.log((7).pluralize('none,one,few,many')); // many
console.log((3).pluralize(['none', 'one', 'few', 'many'])); // few
```

### Number.round([decimals])

Type: `method`

Arrotonda il numero a un determinato numero di cifre decimali.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `decimals` | `Number` | no | `0` | Le cifre decimali da mantenere. |

Returns: `Number` Il numero arrotondato.

Un valore negativo arrotonda alle decine, alle centinaia e così via.

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