Hướng dẫn what happens when you convert a float to an int python? - điều gì xảy ra khi bạn chuyển đổi float thành int python?

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: int
0
type: float
converted value: 9 , type: int
1

type: float
converted value: 9 , type: int
2
type: float
converted value: 9 , type: int
3
type: float
converted value: 9 , type: int
4

num =

type: float
converted value: 9 , type: int
7
type: float
converted value: 9 , type: int
8

print(

the result using floor() :  5 , type :  int
the result using ceil()  :  6 , type:  int
1
the result using floor() :  5 , type :  int
the result using ceil()  :  6 , type:  int
2

type: float
converted value: 9 , type: int
2
the result using floor() :  5 , type :  int
the result using ceil()  :  6 , type:  int
4
the result using floor() :  5 , type :  int
the result using ceil()  :  6 , type:  int
5
type: float
converted value: 9 , type: int
3
the result using floor() :  5 , type :  int
the result using ceil()  :  6 , type:  int
7

Đầ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:  int
8=
Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
0

Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
1=
Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
3

the result using floor() :  5 , type :  int
the result using ceil()  :  6 , type:  int
8=
type: float
converted value: 9 , type: int
7
Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
7

Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
1=
type: float
converted value: 9 , type: int
7
Type of value :  int
the result using round  :  -2
Type of data:  int
the result using round  :  5
1

print

Type of value :  int
the result using round  :  -2
Type of data:  int
the result using round  :  5
3=
Type of value :  int
the result using round  :  -2
Type of data:  int
the result using round  :  5
5
Type of value :  int
the result using round  :  -2
Type of data:  int
the result using round  :  5
6

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  :  5
7
Type of value :  int
the result using round  :  -2
Type of data:  int
the result using round  :  5
8

num = num 1

print(=0

type: float
converted value: 9 , type: int
1

type: float
converted value: 9 , type: int
2=3

type: float
converted value: 9 , type: int
2=5
type: float
converted value: 9 , type: int
1
type: float
converted value: 9 , type: int
3=8

print(9.31

type: float
converted value: 9 , type: int
1

type: float
converted value: 9 , type: int
29.34

type: float
converted value: 9 , type: int
29.36
the result using floor() :  5 , type :  int
the result using ceil()  :  6 , type:  int
5
type: float
converted value: 9 , type: int
39.39

Đầ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:  int
8=
Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
0

Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
1=
Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
3

Parameter:

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.

printprint4print5

the result using floor() :  5 , type :  int
the result using ceil()  :  6 , type:  int
5
type: float
converted value: 9 , type: int
3
the result using floor() :  5 , type :  int
the result using ceil()  :  6 , type:  int
7

printprint4(1(2

Type of value :  int
the result using round  :  -2
Type of data:  int
the result using round  :  5
7
Type of value :  int
the result using round  :  -2
Type of data:  int
the result using round  :  5
8

printprint4print5

type: float
converted value: 9 , type: int
1
type: float
converted value: 9 , type: int
3
type: float
converted value: 9 , type: int
02

print(

type: float
converted value: 9 , type: int
05
type: float
converted value: 9 , type: int
06

Đầ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:  int
8=
Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
0

Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
1=
Type :  float
Original number is :  5.6
Type :  int
the result using round  :  6
3

Parameter:

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  :  5
7
Type of value :  int
the result using round  :  -2
Type of data:  int
the result using round  :  5
8

num = num 1

printprint4

type: float
converted value: 9 , type: int
21
type: float
converted value: 9 , type: int
1
type: float
converted value: 9 , type: int
3
type: float
converted value: 9 , type: int
02

print(

type: float
converted value: 9 , type: int
05
type: float
converted value: 9 , type: int
06

num 2= num 4

printprint4

type: float
converted value: 9 , type: int
34
type: float
converted value: 9 , type: int
1
type: float
converted value: 9 , type: int
3
type: float
converted value: 9 , type: int
37

print(

type: float
converted value: 9 , type: int
05
type: float
converted value: 9 , type: int
41

Đầu ra

Type of value :  int
the result using round  :  -2
Type of data:  int
the result using round  :  5


Điều gì xảy ra khi float được chuyển đổi thành int?

Phương pháp 1: Chuyển đổi bằng int (): Để chuyển đổi giá trị nổi sang 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ố.Ví dụ 1: Số lượng phao loại được chuyển đổi thành kết quả của loại int.trims the values after the decimal point and returns only the integer/whole number part. Example 1: Number of type float is converted to a result of type int.

Chúng ta có thể chuyển đổi nổi sang INT trong Python không?

Giải pháp 1 - Sử dụng int (): Phương pháp chuyển đổi float sang int trong python sử dụng phương thức int () để chuyển đổi phao sang int.Tuy nhiên, phương pháp này dễ bị mất dữ liệu và do đó không được khuyến khích.Như bạn có thể thấy, Python chuyển đổi tất cả các giá trị thành 1 là giá trị số nguyên.

Làm thế nào để hàm INT chuyển đổi một float thành int?

Làm thế nào để hàm INT chuyển đổi một float thành int?Bằng cách làm tròn đến số toàn bộ gần nhất.By rounding to the nearest whole number.

INT và nổi giống nhau trong Python?

Số nguyên là những con số không có dấu thập phân. Các con số là những con số có các điểm thập phân. Floats are numbers with decimal points.