Hướng dẫn can you convert float to integer in python? - bạn có thể chuyển đổi float thành số nguyên trong python không?

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 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 .

Bạn có thể so sánh một chiếc phao với một số nguyên trong Python không?

Nếu số của bạn có số thập phân, Python sẽ tự động coi đó là một chiếc phao.Nếu nó không, nó sẽ tự động coi đó là một số nguyên.Nếu chúng ta muốn thay đổi một chiếc phao thành một số nguyên, chúng ta có thể làm như vậy bằng cách sử dụng hàm int [].Khi chúng ta làm điều này, phao sẽ mất số thập phân và những con số đằng sau vị trí thập phân.If we want to change a float to an integer, we can do so using the int[] function. When we do this, the float will lose its decimal and the numbers behind the decimal place.

Bạn có thể sử dụng phao cho số nguyên?

Có, một giá trị tích phân có thể được thêm vào giá trị nổi.Các hoạt động toán học cơ bản [ +, -, *, /], khi được cung cấp một toán hạng phao và int, INT được chuyển đổi thành float trước.. The basic math operations [ + , - , * , / ], when given an operand of type float and int , the int is converted to float first.

Điều gì sẽ xảy ra nếu bạn đúc một chiếc phao cho một số nguyên?

Đúc một chiếc phao vào một số nguyên cắt ngắn giá trị, vì vậy nếu bạn có 3.999998 và bạn chuyển nó vào một số nguyên, bạn sẽ nhận được 3.truncates the value, so if you have 3.999998 , and you cast it to an integer , you get 3 .

Bài Viết Liên Quan

Chủ Đề