Number
Metodi che jComponent aggiunge a Number.prototype per calcolare, arrotondare e formattare i numeri.
In questa pagina
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, e i template accedono alla stessa formattazione tramite gli helper di Tangular.
API
method
num.add(value, [decimals])
Aggiunge un numero o applica un'operazione scritta come testo.
num.add(value, [decimals])| Parametro | Tipo | Descrizione |
|---|---|---|
value |
Number or String |
Il numero da sommare o l'operazione. |
decimals? |
Number |
Arrotonda per difetto un'operazione testuale a questo numero di decimali. |
Restituisce 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.
(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])
Restituisce il valore assegnato all'intervallo che contiene il numero.
num.between(condition, [otherwise])| Parametro | Tipo | Descrizione |
|---|---|---|
condition |
Object |
Gli intervalli e i relativi valori. |
otherwise? |
Object |
Il valore restituito quando nessun intervallo corrisponde. |
Restituisce 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.
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])
Formatta il numero con un formatter registrato in DEF.currencies.
num.currency([currency], [a], [b], [c])| Parametro | Tipo | Descrizione |
|---|---|---|
currency? |
String |
Il codice della valuta. |
a? |
Object |
Un argomento personalizzato per il formatter. |
b? |
Object |
Un argomento personalizzato per il formatter. |
c? |
Object |
Un argomento personalizzato per il formatter. |
Restituisce 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.
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)
Arrotonda il numero per difetto a un certo numero di decimali.
num.floor(decimals)| Parametro | Tipo | Descrizione |
|---|---|---|
decimals |
Number |
Il numero di decimali. Usa 0 per un numero intero. |
Restituisce Number Il numero arrotondato.
(1.999).floor(2); // 1.99
(5.5).floor(0); // 5
(-1.555).floor(2); // -1.56
method
num.format([decimals], [separator], [decimalSeparator])
Scrive il numero con un separatore delle migliaia e un numero fisso di decimali.
num.format([decimals], [separator], [decimalSeparator])| Parametro | Tipo | Descrizione |
|---|---|---|
decimals? |
Number or String |
Il numero di decimali. |
separator? |
String |
Il separatore delle migliaia. |
decimalSeparator? |
String |
Il separatore decimale. |
Restituisce String Il numero formattato.
I decimali in eccesso vengono troncati, non arrotondati; usa prima 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.
(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])
Scrive il numero come testo e lo riempie a sinistra.
num.padLeft(length, [char])| Parametro | Tipo | Descrizione |
|---|---|---|
length |
Number |
La lunghezza finale. |
char? |
String |
Il carattere da aggiungere. Predefinito: '0' |
Restituisce String Il testo riempito.
(5).padLeft(3); // "005"
(5).padLeft(3, ' '); // " 5"
method
num.padRight(length, [char])
Scrive il numero come testo e lo riempie a destra.
num.padRight(length, [char])| Parametro | Tipo | Descrizione |
|---|---|---|
length |
Number |
La lunghezza finale. |
char? |
String |
Il carattere da aggiungere. Predefinito: '0' |
Restituisce String Il testo riempito.
(5).padRight(3); // "500"
(5).padRight(3, 'x'); // "5xx"
method
num.parseDate([offset])
Crea un Date da un numero di millisecondi.
num.parseDate([offset])| Parametro | Tipo | Descrizione |
|---|---|---|
offset? |
Number |
I millisecondi sommati al numero. Predefinito: 0 |
Restituisce Date La nuova data.
(1700000000000).parseDate().getTime(); // 1700000000000
(1700000000000).parseDate(1000).getTime(); // 1700000001000
method
num.pluralize(zero, one, few, other)
Sceglie un testo in base alla grandezza del numero.
num.pluralize(zero, one, few, other)| Parametro | Tipo | Descrizione |
|---|---|---|
zero |
String or Array |
Il testo per 0, oppure tutte e quattro le forme. |
one |
String |
Il testo per 1. |
few |
String |
Il testo per i numeri maggiori di 1 e minori di 5. |
other |
String |
Il testo per tutti gli altri numeri. |
Restituisce 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().
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.
(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])
Arrotonda il numero a un certo numero di decimali.
num.round([decimals])| Parametro | Tipo | Descrizione |
|---|---|---|
decimals? |
Number |
Il numero di decimali. Predefinito: 0 |
Restituisce Number Il numero arrotondato.
Il numero di decimali non può essere negativo.
(1.005).round(2); // 1.01
(1.5).round(); // 2
(1.4).round(); // 1