KScript 文档
ZH

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 保留两位小数,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。

对于能被 2 整除的整数,->is_even() 返回 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。

对于除以 2 余 1 的整数,->is_odd() 返回 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 日 UTC 零点起经过的秒数,这是许多系统和 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 的方法