KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

number のフィールドとメソッド

整数と小数。

数値は読めるとおりに書きます: 42、3.5、-7。整数は桁数が増えても正確な値を保ちます。そのため 2 ** 100 はすべての桁を表示し、小数点を含む数値は約 15 桁の有効数字で扱われます。

数値メソッドは新しい数値を返し、変数は変更しません。結果を必ず受け取ってください: $total = $total->round(digits: 2)。

数値に直接付けたマイナスは数値の一部です。そのため -5->abs() は 5 と同じです。変数の場合、マイナスはメソッドチェーン全体に適用されます。-$change->abs() は変化量の符号を反転した値で、(-$change)->abs() は先に括弧内を計算します。

数値が範囲内にあるかを確認するには、数学と同じように比較を連ねて書きます。両端を含めるには <= のように書きます:

<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

丸め: round · floor · ceil · to_int

比較と制限: min · max · clamp · abs · is_even · is_odd

ビット: bits

カウント: increment · decrement · times

別のものに変換: format · to_date · to_number

フィールドは、値についての単純な事実で、ドットを使い角括弧なしで読み取ります: $value.name。メソッドは引数を取るか処理を行い、-> で呼び出します: $value->name(arguments)。または、値をパイプで渡して呼び出します: $value |> name(arguments)。

KScript.Core から

#number->abs() → number

符号を除いた値です。

->abs() は数値から符号を除いた値を返します。つまり、ゼロからどれだけ離れているかです。差の大きさだけが必要で向きは問わない場合に適しています:

<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
例
<echo>{ -5->abs() }</echo>
echo -5->abs()

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

整数の各ビットに対して働きます。and: は両方が持つビットを残します(12->bits(and: 10) は 8)。or: はどちらかが持つビットを残します(15)。xor: は片方だけが持つビットを残します(6)。shift: は左にずらし、負の場合は右にずらします(1->bits(shift: 4) は 16)。invert はすべてのビットを反転します(5->bits(invert) は -6)。一度に一つずつ指定します。

->bits(and|or|xor|shift: number) または ->bits(invert) は整数の二進数の桁に対して、呼び出しごとに一段階ずつ働きます。一つの数値に詰め込まれたフラグで、各ビットが一つの意味を持つ場合に適しています:

<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
パラメーター受け取るもの
and optionalnumber
or optionalnumber
xor optionalnumber
shift optionalnumber
invert optionalフラグ: invert または not invert

引数には名前を付けられます: ->bits(…, or: …, xor: …)。

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

#number->ceil() → number

切り上げた値です。

->ceil() は次の整数に切り上げます。整数はそのままです。すべてを収めるのに必要な数を数える場合に適しています:

<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
例
<echo>{ 3.2->ceil() }</echo>
echo 3.2->ceil()

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

min..maxの範囲に制限された数値です。

->clamp(min, max) は数値を min..max の範囲内に収めます。min 未満では min を、max を超えると max を返し、その間では数値そのものを返します:

<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
パラメーター受け取るもの
min requirednumber
max requirednumber

引数には名前を付けられます: ->clamp(…, max: …)。

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

#number->decrement() → number

数値から1を引いた値です。

->decrement() は数値から 1 を引いた値です。->increment() は 1 を足します:

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

#number->floor() → number

切り捨てた値。

->floor() は一つ下の整数に切り下げます。整数はそのままです。整数がいくつ収まるかを数える場合に適しています:

<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
例
<echo>{ 3.7->floor() }</echo>
echo 3.7->floor()

#number->format(pattern?) → string

数値を書式設定します(.NET の書式指定子。既定は N0 で、例: 1,234)。

->format(pattern?) は数値を人が読める文字列に変換します。パターンを省略すると、千の位にカンマを入れた整数になります。パターンを指定すると別の書式を選べます。N2 は小数点以下 2 桁を保持し、P0 は割合をパーセントで表示し、0.00 または 000 はゼロで埋めます:

<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
例
<echo>{ 1234567.891->format('N2') }</echo>
echo 1234567.891->format('N2')

#number->increment() → number

数値に 1 を加えたものです。

->increment() は数値に 1 を足した値です。->decrement() は 1 を引きます:

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

#number->is_even() → bool

数値が偶数の場合に true を返します。

->is_even() は 2 で割り切れる整数のとき true になります。小数を含む数値は偶数でも奇数でもありません:

<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
例
<echo>{ 10->is_even() }</echo>
echo 10->is_even()

#number->is_odd() → bool

数値が奇数の場合に true を返します。

->is_odd() は 2 で割ると余りが 1 になる整数のとき true になります。一行おきに縞模様を付ける場合に適しています:

<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
例
<echo>{ 10->is_odd() }</echo>
echo 10->is_odd()

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

二つのうち大きい方です。

->max(other) は数値と other のうち大きい方を返します。下限を設定する場合に適しています。結果は 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
パラメーター受け取るもの
other 必須number
例
<echo>{ 3->max(10) }</echo>
echo 3->max(10)

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

二つのうち小さい方です。

->min(other) は数値と other のうち小さい方を返します。上限を設定する場合に適しています。結果は 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
パラメーター受け取るもの
other 必須number
例
<echo>{ 12->min(10) }</echo>
echo 12->min(10)

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

小数点以下の桁数 (既定は 0) に丸めた値です。

->round(digits?) は最も近い整数に丸めます。または小数点以下 digits 桁に丸めます。ちょうど中間の値は、紙の上での計算と同じく、ゼロから遠ざかる方向に丸めます:

<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
パラメーター受け取るもの
digits optionalnumber
例
<echo>{ 3.14159->round(digits: 2) }</echo>
echo 3.14159->round(digits: 2)

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

i を 0 から指定の数まで変えながら fn(i) を呼び出し、その結果を返します。

->times(fn(i)) は 0 から、その数値より 1 小さい整数までの各整数について関数を一度ずつ呼び出し、返された値のリストを返します。固定長のリストを作る場合に適しています:

<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
パラメーター受け取るもの
fn 必須fn
例
<echo>{ 3->times(fn(i) => $i * $i) }</echo>
echo 3->times(fn(i) => $i * $i)

#number->to_date() → date

数値を Unix タイムスタンプとして読み取ります。1970-01-01 UTC からの秒数です: 1795996800->to_date() は 2026 年 11 月 30 日になります。->timestamp() は日付を再び数値に戻します。

->to_date() は数値を Unix タイムスタンプ、つまり協定世界時の 1970 年 1 月 1 日0時からの秒数として読み取ります。多くのシステムや API が日付をこの形式で送ります。$date->timestamp() は日付をこの形式の数値に戻します。この方法で日付に変換できるのは整数だけです。日付を進めるには、代わりに 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()
例
<echo>{ 1795996800->to_date()->timestamp() }</echo>
echo 1795996800->to_date()->timestamp()

#number->to_int() → number

整数部分。

->to_int() は小数点より後ろを切り捨て、整数部分だけを残します。->floor() と異なり、負の数ではゼロに向かって丸めます:

<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
例
<echo>{ 9.99->to_int() }</echo>
echo 9.99->to_int()

#number->to_number(default?) → number

数値そのもの。

->to_number(default?) は数値そのものを返します。数値かもしれないし文字列かもしれない値に対して ->to_number() を呼び出せるようにするためにあります。文字列の場合は、そこに書かれた数値を読み取ります:

<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
例
<echo>{ '42'->to_number() + 1 }</echo>
echo '42'->to_number() + 1

KScript.Math から

これらには <import package="KScript.Math" />import KScript.Math が必要です。KScript.Math を参照してください。

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

平方根を返します。
例
<import package="KScript.Math" />
<echo>{ 16->sqrt() }</echo>
import KScript.Math
echo 16->sqrt()

any のメソッド