Chuyển đổi giá trị nổi thành INT được thực hiện bằng cách chuyển đổi loại, đây là một phương pháp rõ ràng để chuyển đổi một toán hạng thành một loại cụ thể. Tuy nhiên, cần lưu ý rằng loại chuyển đổi như vậy có thể có xu hướng mất mát [mất dữ liệu]. Chuyển đổi giá trị INT như 2 thành điểm nổi sẽ dẫn đến 2.0, các loại chuyển đổi đó an toàn vì sẽ không mất dữ liệu, nhưng chuyển đổi 3,4 thành giá trị INT sẽ dẫn đến 3 dẫn đến chuyển đổi mất. & NBSP; Ví dụ : & nbsp;float value to an int is done by Type conversion, which is an explicit method of converting an operand to a specific type. However, it is to be noted that such type of conversion may tend to be a lossy one [loss of data]. Converting an int value like 2 to floating-point will result in 2.0, such types of
conversion are safe as there would be no loss of data, but converting 3.4 to an int value will result in 3 leading to a lossy conversion.
Examples:
Input: 3.3 Output: 3 Input: 5.99 Output: 5
Phương pháp 1: Chuyển đổi bằng int []: Conversion using int[]:
Để chuyển đổi giá trị nổi thành int, chúng tôi sử dụng hàm int [] tích hợp, hàm này cắt các giá trị sau điểm thập phân và chỉ trả về phần số nguyên/toàn bộ số. built-in int[] function, this function trims the values after the decimal point and returns only the integer/whole number part.
Syntax: int[x] int[x]
Trả về: Giá trị số nguyên integer value
Ví dụ 1: Số lượng phao loại được chuyển đổi thành kết quả của loại int.Number of type float is converted to a result of type int.
Python3
num
=
9.3
print
[
type: float converted value: 9 , type: int0
type: float converted value: 9 , type: int1
type: float converted value: 9 , type: int2
type: float converted value: 9 , type: int3
type: float converted value: 9 , type: int4
num
=
type: float converted value: 9 , type: int7
type: float converted value: 9 , type: int8
print
[
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int1
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int2
type: float converted value: 9 , type: int2
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int4
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int5
type: float converted value: 9 , type: int3
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int7
Đầu ra
type: float converted value: 9 , type: int
Ví dụ 2: Trong hầu hết các trường hợp, hàm int [] làm tròn kết quả với số nguyên nhỏ hơn hoặc bằng đầu vào, nhưng hành vi không xác định cũng không thể dự đoán được. Một ví dụ như vậy được hiển thị dưới đây.In most cases the int[] function rounds off the result to an integer lesser than or equal to the input, but the behavior is neither definite nor predictable. One such example is shown below.
Python3
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int8
=
Type : float Original number is : 5.6 Type : int the result using round : 60
Type : float Original number is : 5.6 Type : int the result using round : 61
=
Type : float Original number is : 5.6 Type : int the result using round : 63
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int8
=
type: float converted value: 9 , type: int7
Type : float Original number is : 5.6 Type : int the result using round : 67
Type : float Original number is : 5.6 Type : int the result using round : 61
=
type: float converted value: 9 , type: int7
Type of value : int the result using round : -2 Type of data: int the result using round : 51
print
Type of value : int the result using round : -2 Type of data: int the result using round : 53
=
Type of value : int the result using round : -2 Type of data: int the result using round : 55
Type of value : int the result using round : -2 Type of data: int the result using round : 56
Phương pháp 2: Chuyển đổi bằng Math.Floor [] và Math.ceil []. Conversion using math.floor[] and math.ceil[].
Một giá trị float có thể được chuyển đổi thành giá trị INT không lớn hơn đầu vào bằng cách sử dụng hàm math.floor [], trong khi nó cũng có thể được chuyển đổi thành giá trị INT là số nguyên nhỏ nhất lớn hơn đầu vào sử dụng math.ceil [] hàm số. Mô -đun toán học sẽ được nhập để sử dụng các phương pháp này.
Cú pháp: Math.Floor [x] math.floor[x]
Parameter:
X: Đây là một biểu thức số.This is a numeric expression.
Trả về: Số nguyên lớn nhất không lớn hơn x. largest integer not greater than x.
Cú pháp: math.ceil [x] math.ceil[x]
Parameter:
X: Đây là một biểu thức số. This is a numeric expression.
Trả về: Số nguyên lớn nhất không lớn hơn x. Smallest integer not less than x.
Cú pháp: math.ceil [x]In the below example conversion from float to int has been achieved using the floor[] and ceil[] methods, the former returns an int no larger than the input and the latter returns the smallest integer larger than the input.
Python3
Trả về: Số nguyên nhỏ nhất không nhỏ hơn x.
Ví dụ: Trong chuyển đổi ví dụ dưới đây từ phao sang INT đã đạt được bằng cách sử dụng các phương thức sàn [] và ceil [], trước đây trả về INT không lớn hơn đầu vào và phần sau trả về số nguyên nhỏ nhất lớn hơn đầu vào.
Type of value : int the result using round : -2 Type of data: int the result using round : 57
Type of value : int the result using round : -2 Type of data: int the result using round : 58
num
=
num
1
print
[
=
0
type: float converted value: 9 , type: int1
type: float converted value: 9 , type: int2
=
3type: float converted value: 9 , type: int2
=
5type: float converted value: 9 , type: int1
type: float converted value: 9 , type: int3
=
8print
[
9.3
1
type: float converted value: 9 , type: int1
type: float converted value: 9 , type: int2
9.3
4type: float converted value: 9 , type: int2
9.3
6the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int5
type: float converted value: 9 , type: int3
9.3
9Đầu ra
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int
Ví dụ 2: Trong hầu hết các trường hợp, hàm int [] làm tròn kết quả với số nguyên nhỏ hơn hoặc bằng đầu vào, nhưng hành vi không xác định cũng không thể dự đoán được. Một ví dụ như vậy được hiển thị dưới đây.Conversion using round[ ].
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int8
=
Type : float Original number is : 5.6 Type : int the result using round : 60
Type : float Original number is : 5.6 Type : int the result using round : 61=
Type : float Original number is : 5.6 Type : int the result using round : 63Parameter:
X: Đây là một biểu thức số.
Trả về: Số nguyên lớn nhất không lớn hơn x.
Cú pháp: math.ceil [x]
Python3
Ví dụ: Trong chuyển đổi ví dụ dưới đây từ phao sang INT đã đạt được bằng cách sử dụng các phương thức sàn [] và ceil [], trước đây trả về INT không lớn hơn đầu vào và phần sau trả về số nguyên nhỏ nhất lớn hơn đầu vào.
print
print
4print
5
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int5
type: float converted value: 9 , type: int3
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int7
print
print
4[
1[
2
Type of value : int the result using round : -2 Type of data: int the result using round : 57
Type of value : int the result using round : -2 Type of data: int the result using round : 58
print
print
4print
5
type: float converted value: 9 , type: int1
type: float converted value: 9 , type: int3
type: float converted value: 9 , type: int02
print
[
type: float converted value: 9 , type: int05
type: float converted value: 9 , type: int06
Đầu ra
Type : float Original number is : 5.6 Type : int the result using round : 6
Ví dụ 2: Trong hầu hết các trường hợp, hàm int [] làm tròn kết quả với số nguyên nhỏ hơn hoặc bằng đầu vào, nhưng hành vi không xác định cũng không thể dự đoán được. Một ví dụ như vậy được hiển thị dưới đây. Conversion using math.trunc[ ].
the result using floor[] : 5 , type : int the result using ceil[] : 6 , type: int8
=
Type : float Original number is : 5.6 Type : int the result using round : 60
Type : float Original number is : 5.6 Type : int the result using round : 61=
Type : float Original number is : 5.6 Type : int the result using round : 63Parameter:
X: Đây là một biểu thức số.
Trả về: Số nguyên lớn nhất không lớn hơn x.
Cú pháp: math.ceil [x] In the below example conversion from float to int has been achieved using the math.trunc[] methods, the former returns an larger int number which in case of negative number, else in case of positive number return smaller integer number.
Python3
Trả về: Số nguyên nhỏ nhất không nhỏ hơn x.
Ví dụ: Trong chuyển đổi ví dụ dưới đây từ phao sang INT đã đạt được bằng cách sử dụng các phương thức sàn [] và ceil [], trước đây trả về INT không lớn hơn đầu vào và phần sau trả về số nguyên nhỏ nhất lớn hơn đầu vào.
Type of value : int the result using round : -2 Type of data: int the result using round : 57
Type of value : int the result using round : -2 Type of data: int the result using round : 58
num
=
num
1
print
print
4
type: float converted value: 9 , type: int21
type: float converted value: 9 , type: int1
type: float converted value: 9 , type: int3
type: float converted value: 9 , type: int02
print
[
type: float converted value: 9 , type: int05
type: float converted value: 9 , type: int06
num
2=
num
4
print
print
4
type: float converted value: 9 , type: int34
type: float converted value: 9 , type: int1
type: float converted value: 9 , type: int3
type: float converted value: 9 , type: int37
print
[
type: float converted value: 9 , type: int05
type: float converted value: 9 , type: int41
Đầu ra
Type of value : int the result using round : -2 Type of data: int the result using round : 5