KScript documentazione
IT

Lingua

Tradotto automaticamente dall’inglese, che è la versione originale.

Campi e metodi di number

Numeri interi e decimali.

Un numero si scrive come si legge: 42, 3.5, -7. I numeri interi restano esatti anche quando crescono, quindi 2 ** 100 mostra ogni cifra, mentre i numeri con la virgola sono precisi fino a circa 15 cifre significative.

I metodi dei numeri restituiscono un nuovo numero e non modificano mai una variabile, quindi conserva il risultato: $total = $total->round(digits: 2).

Un meno scritto direttamente davanti a un numero fa parte del numero, quindi -5->abs() equivale a 5. Con una variabile, il meno si applica a tutta la catena: -$change->abs() è il meno della dimensione della variazione, mentre (-$change)->abs() la racchiude prima tra parentesi.

Per verificare che un numero sia in un intervallo, concatena i confronti come in matematica. Entrambi gli estremi sono inclusi con <=:

<let name="age" value="18" />
<echo value="13 &lt;= $age &lt;= 19" /> <!-- true because 18 is a teenage age -->
<echo value="0 &lt;= $age &lt;= 17" /> <!-- false because 18 is past 17 -->
$age = 18
echo 13 <= $age <= 19   // true because 18 is a teenage age
echo 0 <= $age <= 17    // false because 18 is past 17

Arrotondamento: round · floor · ceil · to_int

Confronto e limiti: min · max · clamp · abs · is_even · is_odd

Bit: bits

Conteggio: increment · decrement · times

Trasforma in qualcos'altro: format · to_date · to_number

Un campo è un dato semplice sul valore, letto con un punto e senza parentesi: $value.name. Un metodo richiede argomenti o esegue un'operazione, e si chiama con ->: $value->name(arguments), oppure passando il valore con la pipe: $value |> name(arguments).

Da KScript.Core

#number->abs() → number

Senza il suo segno.

->abs() restituisce il numero senza il segno meno: quanto dista dallo zero. È adatto a una differenza in cui ti interessa quanto è grande, ma non in quale direzione è andata:

<let name="change" value="4 - 9" />
<echo value="$change" /> <!-- -5 -->
<echo value="$change->abs()" /> <!-- 5 because the sign is dropped -->
<echo value="-5->abs()" /> <!-- 5 because -5 is one number, and its size is 5 -->
$change = 4 - 9
echo $change          // -5
echo $change->abs()   // 5 because the sign is dropped
echo -5->abs()        // 5 because -5 is one number, and its size is 5
Esempio
<echo>{ -5->abs() }</echo>
echo -5->abs()

#number->bits(and?: number, or?: number, xor?: number, shift?: number, invert?: bool) → number

Lavora sui bit del numero, come numero intero: and: mantiene i bit che sono presenti in entrambi (12->bits(and: 10) è 8), or: i bit presenti in almeno uno (15), xor: i bit presenti in uno solo (6), shift: li sposta a sinistra, o a destra se negativo (1->bits(shift: 4) è 16), e invert inverte ogni bit (5->bits(invert) è -6). Uno alla volta.

->bits(and|or|xor|shift: number) o ->bits(invert) lavora sulle cifre binarie di un numero intero, un passo per chiamata. È adatto ai flag compressi in un solo numero, dove ogni bit indica una cosa:

<let name="read" value="4" />
<let name="write" value="2" />
<let name="permissions" value="6" /> <!-- read and write, because 4 + 2 is 6 -->
<echo value="$permissions->bits(and: $read) == $read" /> <!-- true because 6 has the 4 bit set -->
<echo value="$permissions->bits(or: 1)" /> <!-- 7 because or: turns the 1 bit on -->
<echo value="$permissions->bits(xor: $write)" /> <!-- 4 because xor: flips the 2 bit off -->
<echo value="$permissions->bits(shift: 1)" /> <!-- 12 because every bit moves one place left -->
<echo value="$permissions->bits(shift: -1)" /> <!-- 3 because a negative shift moves right -->
<echo value="$permissions->bits(invert)" /> <!-- -7 because every bit flips -->
$read = 4
$write = 2
$permissions = 6                              // read and write, because 4 + 2 is 6
echo $permissions->bits(and: $read) == $read // true because 6 has the 4 bit set
echo $permissions->bits(or: 1)               // 7 because or: turns the 1 bit on
echo $permissions->bits(xor: $write)         // 4 because xor: flips the 2 bit off
echo $permissions->bits(shift: 1)            // 12 because every bit moves one place left
echo $permissions->bits(shift: -1)           // 3 because a negative shift moves right
echo $permissions->bits(invert)              // -7 because every bit flips
ParametroAccetta
and optionalnumber
or optionalnumber
xor optionalnumber
shift optionalnumber
invert optionalun flag: invert o not invert

Gli argomenti possono avere un nome: ->bits(…, or: …, xor: …).

Esempio
<echo>{ 12->bits(and: 10) }</echo>
echo 12->bits(and: 10)

#number->ceil() → number

Arrotondato per eccesso.

->ceil() arrotonda per eccesso al numero intero successivo, oppure lascia invariato un numero intero. È adatto a contare quanto serve per contenere tutto:

<let name="people" value="23" />
<echo value="($people / 4)->ceil()" /> <!-- 6 because 23 / 4 is 5.75, and 5 tables of 4 would leave 3 people standing -->
<echo value="6->ceil()" /> <!-- 6 because it is already whole -->
<echo value="-2.5->ceil()" /> <!-- -2 because up means towards the larger number -->
$people = 23
echo ($people / 4)->ceil()   // 6 because 23 / 4 is 5.75, and 5 tables of 4 would leave 3 people standing
echo 6->ceil()               // 6 because it is already whole
echo -2.5->ceil()          // -2 because up means towards the larger number
Esempio
<echo>{ 3.2->ceil() }</echo>
echo 3.2->ceil()

#number->clamp(min: number, max: number) → number

Il numero limitato all'intervallo min..max.

->clamp(min, max) mantiene il numero all'interno di min..max. Sotto min restituisce min, sopra max restituisce max, e nel mezzo restituisce il numero:

<echo value="150->clamp(0, 100)" /> <!-- 100 because 150 is above the top -->
<echo value="-20->clamp(0, 100)" /> <!-- 0 because -20 is below the bottom -->
<echo value="42->clamp(0, 100)" /> <!-- 42 because it is already in range -->
echo 150->clamp(0, 100)   // 100 because 150 is above the top
echo -20->clamp(0, 100) // 0 because -20 is below the bottom
echo 42->clamp(0, 100)    // 42 because it is already in range
ParametroAccetta
min requirednumber
max requirednumber

Gli argomenti possono avere un nome: ->clamp(…, max: …).

Esempio
<echo>{ 150->clamp(0, 100) }</echo>
echo 150->clamp(0, 100)

#number->decrement() → number

Il numero meno 1.

->decrement() è il numero meno 1. ->increment() aggiunge 1:

<let name="lives" value="3" />
<set name="lives" value="$lives->decrement()" />
<echo value="$lives" /> <!-- 2 -->
$lives = 3
$lives = $lives->decrement()
echo $lives   // 2
Esempio
<echo>{ 43->decrement() }</echo>
echo 43->decrement()

#number->floor() → number

Arrotondato per difetto.

->floor() arrotonda per difetto al numero intero inferiore, oppure lascia invariato un numero intero. È adatto a contare quante unità intere ci stanno:

<let name="budget" value="20" />
<echo value="($budget / 3)->floor()" /> <!-- 6 because 20 / 3 is 6.67, and only 6 whole tickets at £3 fit in £20 -->
<echo value="3.99->floor()" /> <!-- 3 because the decimals are always dropped downwards -->
<echo value="-2.5->floor()" /> <!-- -3 because down means towards the smaller number -->
$budget = 20
echo ($budget / 3)->floor()   // 6 because 20 / 3 is 6.67, and only 6 whole tickets at £3 fit in £20
echo 3.99->floor()            // 3 because the decimals are always dropped downwards
echo -2.5->floor()          // -3 because down means towards the smaller number
Esempio
<echo>{ 3.7->floor() }</echo>
echo 3.7->floor()

#number->format(pattern?) → string

Formatta il numero (formato .NET, N0 per impostazione predefinita: 1,234).

->format(pattern?) trasforma il numero in testo leggibile. Senza schema restituisce un numero intero con virgole tra le migliaia. Uno schema sceglie un altro formato: N2 mantiene due decimali, P0 mostra una quota come percentuale, e 0.00 o 000 riempiono con zeri:

<echo value="1234567->format()" /> <!-- '1,234,567' because the commas split it into thousands -->
<echo value="1234.567->format('N2')" /> <!-- '1,234.57' because N2 keeps two decimal places, rounded -->
<echo value="0.075->format('P1')" /> <!-- '7.5 %' because P shows a share of 1 as a percentage -->
<echo value="7->format('000')" /> <!-- '007' because each 0 is a digit that must be shown -->
echo 1234567->format()           // '1,234,567' because the commas split it into thousands
echo 1234.567->format('N2')      // '1,234.57' because N2 keeps two decimal places, rounded
echo 0.075->format('P1')         // '7.5 %' because P shows a share of 1 as a percentage
echo 7->format('000')            // '007' because each 0 is a digit that must be shown
Esempio
<echo>{ 1234567.891->format('N2') }</echo>
echo 1234567.891->format('N2')

#number->increment() → number

Il numero più 1.

->increment() è il numero più 1. ->decrement() sottrae 1:

<let name="visits" value="41" />
<set name="visits" value="$visits->increment()" />
<echo value="$visits" /> <!-- 42 -->
$visits = 41
$visits = $visits->increment()
echo $visits   // 42
Esempio
<echo>{ 41->increment() }</echo>
echo 41->increment()

#number->is_even() → bool

Vero quando il numero è pari.

->is_even() è vero per un numero intero che 2 divide esattamente. Un numero con decimali non è né pari né dispari:

<echo value="10->is_even()" /> <!-- true -->
<echo value="7->is_even()" /> <!-- false because 7 leaves 1 over when shared into twos -->
<echo value="-4->is_even()" /> <!-- true because negative numbers can be even too -->
<echo value="2.5->is_even()" /> <!-- false because it isn't a whole number -->
echo 10->is_even()        // true
echo 7->is_even()         // false because 7 leaves 1 over when shared into twos
echo -4->is_even()      // true because negative numbers can be even too
echo 2.5->is_even()       // false because it isn't a whole number
Esempio
<echo>{ 10->is_even() }</echo>
echo 10->is_even()

#number->is_odd() → bool

Vero quando il numero è dispari.

->is_odd() è vero per un numero intero che, diviso per 2, lascia resto 1. È adatto a colorare a strisce una riga sì e una no:

<let name="rows" value="['tea', 'milk', 'juice']" />
<echo value="$rows->map(fn(drink, index) => $index->is_odd() ? 'grey' : 'white')" /> <!-- ['white', 'grey', 'white'] because rows 0 and 2 are even -->
<echo value="2.5->is_odd()" /> <!-- false because it isn't a whole number -->
$rows = ['tea', 'milk', 'juice']
echo $rows->map(fn(drink, index) => $index->is_odd() ? 'grey' : 'white')   // ['white', 'grey', 'white'] because rows 0 and 2 are even
echo 2.5->is_odd()   // false because it isn't a whole number
Esempio
<echo>{ 10->is_odd() }</echo>
echo 10->is_odd()

#number->max(other: number) → number

Il maggiore dei due.

->max(other) restituisce il maggiore tra il numero e other. È adatto a fissare un minimo: il risultato non scende mai sotto other:

<let name="balance" value="-12" />
<echo value="$balance->max(0)" /> <!-- 0 because 0 is larger than -12, so the shown balance never goes below 0 -->
<echo value="30->max(0)" /> <!-- 30 -->
$balance = -12
echo $balance->max(0)   // 0 because 0 is larger than -12, so the shown balance never goes below 0
echo 30->max(0)         // 30
ParametroAccetta
other obbligatorionumber
Esempio
<echo>{ 3->max(10) }</echo>
echo 3->max(10)

#number->min(other: number) → number

Il minore dei due.

->min(other) restituisce il minore tra il numero e other. È adatto a fissare un massimo: il risultato non supera mai other:

<let name="wanted" value="12" />
<let name="in_stock" value="10" />
<echo value="$wanted->min($in_stock)" /> <!-- 10 because you can't sell more than the 10 there are -->
$wanted = 12
$in_stock = 10
echo $wanted->min($in_stock)   // 10 because you can't sell more than the 10 there are
ParametroAccetta
other obbligatorionumber
Esempio
<echo>{ 12->min(10) }</echo>
echo 12->min(10)

#number->round(digits?: number) → number

Arrotondato a un numero di cifre decimali (0 per impostazione predefinita).

->round(digits?) arrotonda al numero intero più vicino, oppure a digits decimali. Un numero esattamente a metà si arrotonda allontanandosi dallo zero, come si farebbe su carta:

<echo value="3.14159->round(digits: 2)" /> <!-- 3.14 because the next digit, 1, rounds down -->
<echo value="2.5->round()" /> <!-- 3 because halfway rounds away from zero -->
<echo value="-2.5->round()" /> <!-- -3 because halfway rounds away from zero on the negative side too -->
<echo value="9.996->round(digits: 2)" /> <!-- 10 because 9.996 is nearer 10.00 than 9.99 -->
echo 3.14159->round(digits: 2)   // 3.14 because the next digit, 1, rounds down
echo 2.5->round()                // 3 because halfway rounds away from zero
echo -2.5->round()             // -3 because halfway rounds away from zero on the negative side too
echo 9.996->round(digits: 2)     // 10 because 9.996 is nearer 10.00 than 9.99
ParametroAccetta
digits optionalnumber
Esempio
<echo>{ 3.14159->round(digits: 2) }</echo>
echo 3.14159->round(digits: 2)

#number->times(fn(i)) → list

Chiama fn(i) per i da 0 fino al numero, e restituisce i risultati.

->times(fn(i)) chiama la funzione una volta per ogni numero intero da 0 fino a uno in meno del numero, e restituisce una lista di ciò che ha restituito. È adatto a costruire una lista di lunghezza fissa:

<echo value="3->times(fn(i) => $i * $i)" /> <!-- [0, 1, 4] because i is 0, then 1, then 2 -->
<echo value="4->times(fn(i) => &quot;Seat { $i + 1 }&quot;)->last()" /> <!-- 'Seat 4' because i stops at 3 -->
<echo value="0->times(fn(i) => $i)" /> <!-- [] because there is nothing to count -->
echo 3->times(fn(i) => $i * $i)          // [0, 1, 4] because i is 0, then 1, then 2
echo 4->times(fn(i) => "Seat { $i + 1 }")->last() // 'Seat 4' because i stops at 3
echo 0->times(fn(i) => $i)               // [] because there is nothing to count
ParametroAccetta
fn obbligatoriofn
Esempio
<echo>{ 3->times(fn(i) => $i * $i) }</echo>
echo 3->times(fn(i) => $i * $i)

#number->to_date() → date

Legge il numero come timestamp Unix, secondi trascorsi dal 1970-01-01 UTC: 1795996800->to_date() è il 30 novembre 2026. ->timestamp() riporta una data al suo timestamp.

->to_date() legge il numero come timestamp Unix, il numero di secondi trascorsi dall'inizio del 1 gennaio 1970 in UTC, che è il formato con cui molti sistemi e API inviano le date. $date->timestamp() riporta una data al suo timestamp. In questo modo solo i numeri interi possono diventare date; per spostare una data in avanti, aggiungi invece una durata come 3d:

<let name="sent" value="1795996800->to_date()" />
<echo value="$sent->format('d MMMM yyyy')" /> <!-- '30 November 2026' -->
<echo value="$sent->timestamp()" /> <!-- 1795996800 because ->timestamp() reverses ->to_date() -->
$sent = 1795996800->to_date()
echo $sent->format('d MMMM yyyy')   // '30 November 2026'
echo $sent->timestamp()             // 1795996800 because ->timestamp() reverses ->to_date()
Esempio
<echo>{ 1795996800->to_date()->timestamp() }</echo>
echo 1795996800->to_date()->timestamp()

#number->to_int() → number

La parte intera.

->to_int() elimina tutto ciò che sta dopo il punto decimale, lasciando la parte intera. A differenza di ->floor(), per un numero negativo va verso lo zero:

<echo value="9.99->to_int()" /> <!-- 9 because the .99 is dropped, not rounded -->
<echo value="-3.7->to_int()" /> <!-- -3 because the decimals are dropped, which moves towards zero -->
<echo value="-3.7->floor()" /> <!-- -4 because ->floor() always goes down -->
echo 9.99->to_int()      // 9 because the .99 is dropped, not rounded
echo -3.7->to_int()    // -3 because the decimals are dropped, which moves towards zero
echo -3.7->floor()     // -4 because ->floor() always goes down
Esempio
<echo>{ 9.99->to_int() }</echo>
echo 9.99->to_int()

#number->to_number(default?) → number

Il numero stesso.

->to_number(default?) restituisce il numero stesso. Serve perché un valore che potrebbe essere un numero o un testo possa chiamare ->to_number() in entrambi i casi. Su un testo legge il numero scritto al suo interno:

<echo value="42->to_number()" /> <!-- 42 -->
<echo value="'12.5'->to_number() + 1" /> <!-- 13.5 because the text is read as a number first -->
<echo value="'none'->to_number(0)" /> <!-- 0 because 'none' isn't a number, so the default is used -->
echo 42->to_number()                  // 42
echo '12.5'->to_number() + 1          // 13.5 because the text is read as a number first
echo 'none'->to_number(0)             // 0 because 'none' isn't a number, so the default is used
Esempio
<echo>{ '42'->to_number() + 1 }</echo>
echo '42'->to_number() + 1

Da KScript.Math

Richiedono <import package="KScript.Math" />import KScript.Math. Vedi KScript.Math.

#number->sqrt() → number KScript.Math

La radice quadrata.
Esempio
<import package="KScript.Math" />
<echo>{ 16->sqrt() }</echo>
import KScript.Math
echo 16->sqrt()

Metodi da any