Hướng dẫn how do you convert float to int in python? - làm thế nào để bạn chuyển đổi float thành int trong 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


Làm thế nào để bạn chuyển đổi một chiếc phao sang một int trong python?

Giá trị phao 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 có số nguyên nhỏ nhất lớn hơn đầu vào sử dụng toán học. Hàm trần [].by using the math. floor[] function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil[] function.

Điều gì xảy ra khi bạn chuyển đổi một chiếc phao thành int python?

Khi bạn chuyển đổi một chiếc phao thành một số nguyên, bạn sẽ bị mất dữ liệu.Ví dụ, 20.3 có thể trở thành 20 hoặc 21.you'll have a data loss. For example, 20.3 may become 20 or 21 .

Làm thế nào để bạn chuyển đổi sang INT trong Python?

Để chuyển đổi hoặc đúc, một chuỗi thành một số nguyên trong Python, bạn sử dụng hàm tích hợp int [].Hàm nhận được như một tham số, chuỗi ban đầu bạn muốn chuyển đổi và trả về số nguyên tương đương với giá trị bạn đã vượt qua.Cú pháp chung trông giống như thế này: int ["str"].use the int[] built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int["str"] .

Bạn có thể gán một chiếc phao cho một int?

Một giá trị float có thể được gán cho một biến số nguyên nhưng việc chuyển đổi ngầm xảy ra khi trình biên dịch buộc giá trị float phải được gán như một số nguyên.Các chữ số sau ký hiệu thập phân trong giá trị phao bị mất sau khi gán một chiếc phao cho một số nguyên. but an implicit conversion occurs when compiler forces a float value to be assigned as an integer. The digits after the decimal notation in the float value get lost after assigning a float to an integer.

Bài Viết Liên Quan

Chủ Đề