KScript 문서
KO

언어

영어에서 자동 번역되었습니다. 원문은 영어입니다.

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()는 숫자에서 마이너스 부호를 뺀 값, 즉 0에서 얼마나 떨어져 있는지를 돌려줘요. 차이의 방향은 상관없고 크기만 중요할 때 알맞아요:

<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는 0으로 자릿수를 채워요:

<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

숫자가 짝수일 때 참이에요.

->is_even()는 2로 정확히 나누어떨어지는 정수일 때 참이에요. 소수가 있는 숫자는 짝수도 홀수도 아니에요:

<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

숫자가 홀수이면 참이에요.

->is_odd()는 2로 나눈 나머지가 1인 정수일 때 참이에요. 한 줄씩 건너뛰며 줄무늬를 넣을 때 알맞아요:

<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 소수점 자리까지 반올림해요. 정확히 중간에 있는 수는 종이에서 하듯 0에서 멀어지는 쪽으로 반올림해요:

<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부터 숫자 바로 아래까지의 정수마다 함수를 한 번씩 호출하고, 반환된 값들의 목록을 돌려줘요. 길이가 정해진 목록을 만들 때 알맞아요:

<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 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()와 달리 음수는 0 쪽으로 가져가요:

<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의 메서드