Up to date

This page is up to date for Godot `4.2`. If you still find outdated information, please open an issue.

# int¶

## 描述¶

int 可以在需要时自动转换为 float，例如在作为函数的参数传递的时候。float 会尽可能与原始整数接近。

```var x: int = 1 # x 为 1
x = 4.2 # x 为 4，因为 4.2 发生了截断
var max_int = 9223372036854775807 # int 所能存储的最大值
max_int += 1 # max_int 现在是 -9223372036854775808，因为它绕到了另一端
```

```var x = 0b1001 # x 为 9
var y = 0xF5 # y 为 245
var z = 10_000_000 # z 为 10000000
```

## 构造函数¶

 int int ( ) int int ( int from ) int int ( String from ) int int ( bool from ) int int ( float from )

## 操作符¶

 bool operator != ( float right ) bool operator != ( int right ) int operator % ( int right ) int operator & ( int right ) Color operator * ( Color right ) Quaternion operator * ( Quaternion right ) Vector2 operator * ( Vector2 right ) Vector2i operator * ( Vector2i right ) Vector3 operator * ( Vector3 right ) Vector3i operator * ( Vector3i right ) Vector4 operator * ( Vector4 right ) Vector4i operator * ( Vector4i right ) float operator * ( float right ) int operator * ( int right ) float operator ** ( float right ) int operator ** ( int right ) float operator + ( float right ) int operator + ( int right ) float operator - ( float right ) int operator - ( int right ) float operator / ( float right ) int operator / ( int right ) bool operator < ( float right ) bool operator < ( int right ) int operator << ( int right ) bool operator <= ( float right ) bool operator <= ( int right ) bool operator == ( float right ) bool operator == ( int right ) bool operator > ( float right ) bool operator > ( int right ) bool operator >= ( float right ) bool operator >= ( int right ) int operator >> ( int right ) int operator ^ ( int right ) int int int operator | ( int right ) int

## 构造函数说明¶

int int ( )

int int ( int from )

int int ( String from )

String 构造新的 int，遵循与 String.to_int 相同的规则。

int int ( bool from )

bool 构造新的 int`true` 会转换为 `1``false` 会转换为 `0`

int int ( float from )

float 构造新的 int。这样会截断该 float，丢弃小数点之后的部分。

## 操作符说明¶

bool operator != ( float right )

bool operator != ( int right )

int operator % ( int right )

```print(6 % 2) # 输出 0
print(11 % 4) # 输出 3
print(-5 % 3) # 输出 -2
```

int operator & ( int right )

```print(0b1100 & 0b1010) # 输出 8（二进制 1000）
```

```var flags = 0b101
# 检查是否启用了第一或第二个比特位。
if flags & 0b011:
do_stuff() # 会执行这一行。
```

Color operator * ( Color right )

Color 的每个分量乘以该 int

Quaternion operator * ( Quaternion right )

Quaternion 的每个分量乘以该 int。此操作本身没有意义，但可以用作更大表达式的一部分。

Vector2 operator * ( Vector2 right )

Vector2 的每个分量乘以该 float

```print(2 * Vector2(1, 4)) # 输出 (2, 8)
```

Vector2i operator * ( Vector2i right )

Vector2i 的每个分量乘以该 int

Vector3 operator * ( Vector3 right )

Vector3 的每个分量乘以该 int

Vector3i operator * ( Vector3i right )

Vector3i 的每个分量乘以该 int

Vector4 operator * ( Vector4 right )

Vector4 的每个分量乘以该 int

Vector4i operator * ( Vector4i right )

Vector4i 的每个分量乘以该 int

float operator * ( float right )

float 和该 int 相乘。结果为 float

int operator * ( int right )

float operator ** ( float right )

int 提升到 float 次幂。结果为 float

```print(2 ** 0.5) # 输出 1.4142135623731
```

int operator ** ( int right )

```print(3 ** 4) # 输出 81
```

float operator + ( float right )

int operator + ( int right )

float operator - ( float right )

int operator - ( int right )

float operator / ( float right )

```print(10 / 3.0) # 输出 3.33333333333333
```

int operator / ( int right )

```print(6 / 2) # 输出 3
print(5 / 3) # 输出 1
```

bool operator < ( float right )

bool operator < ( int right )

int operator << ( int right )

```print(0b1010 << 1) # 输出 20（二进制 10100）
print(0b1010 << 3) # 输出 80（二进制 1010000）
```

bool operator <= ( float right )

bool operator <= ( int right )

bool operator == ( float right )

bool operator == ( int right )

bool operator > ( float right )

bool operator > ( int right )

bool operator >= ( float right )

bool operator >= ( int right )

int operator >> ( int right )

```print(0b1010 >> 1) # 输出 5（二进制 101）
print(0b1010 >> 2) # 输出 2（二进制 10）
```

int operator ^ ( int right )

```print(0b1100 ^ 0b1010) # 输出 6（二进制 110）
```

int operator unary+ ( )

int operator unary- ( )

int operator | ( int right )

```print(0b1100 | 0b1010) # 输出 14（二进制 1110）
```

```var flags = 0
flags |= 0b101 # 置第一和第三位。
```

int operator ~ ( )

```print(~4) # 输出 -5
print(~(-7)) # 输出 6
```