To convert a floating-point number to an integer in Python, you can use the int()
function. Here’s an example:
x = 3.14 y = int(x) print(y) # Output: 3
In this example, we assign the value 3.14
to the variable x
. Then we use the int()
function to convert x
to an integer and assign the result to the variable y
. Finally, we print the value of y
, which is 3
.
It’s important to note that when you convert a floating-point number to an integer using the int()
function, Python truncates the decimal part of the number. This means that int(3.14)
will return 3
, but int(3.99)
will also return 3
.
Using trunc()
You can also use the trunc()
method from the math
module to convert a floating-point number to an integer by truncating its decimal part. Here’s an example:
import math x = 3.14 y = math.trunc(x) print(y) # Output: 3
In this example, we import the math
module and then assign the value 3.14
to the variable x
. Then we use the math.trunc()
method to convert x
to an integer by truncating its decimal part and assign the result to the variable y
. Finally, we print the value of y
, which is 3
.
It’s worth noting that math.trunc()
works in the same way as using the int()
function, i.e., it simply truncates the decimal part of the number.
Using floor()
You can also use the floor()
method from the math
module to convert a floating-point number to an integer by rounding down to the nearest integer. Here’s an example:
import math x = 3.14 y = math.floor(x) print(y) # Output: 3
In this example, we import the math
module and then assign the value 3.14
to the variable x
. Then we use the math.floor()
method to convert x
to an integer by rounding it down to the nearest integer and assign the result to the variable y
. Finally, we print the value of y
, which is 3
.
It’s worth noting that math.floor()
differs from using the int()
function or math.trunc()
method in that it always rounds down to the nearest integer, even if the original number is already negative. For example, math.floor(-3.14)
would return -4
, whereas int(-3.14)
and math.trunc(-3.14)
would both return -3
.
Using ceil()
You can use the ceil()
method from the math
module to convert a floating-point number to an integer by rounding up to the nearest integer. Here’s an example:
import math x = 3.14 y = math.ceil(x) print(y) # Output: 4
In this example, we import the math
module and then assign the value 3.14
to the variable x
. Then we use the math.ceil()
method to convert x
to an integer by rounding it up to the nearest integer and assign the result to the variable y
. Finally, we print the value of y
, which is 4
.
It’s worth noting that math.ceil()
differs from using the int()
function or math.trunc()
method in that it always rounds up to the nearest integer, even if the original number is already positive and has no decimal part. For example, math.ceil(3.0)
would return 3
, whereas int(3.0)
and math.trunc(3.0)
would both return 3
.
Conclusion:
In conclusion, there are multiple ways to convert a floating-point number to an integer in Python depending on the desired rounding behavior.
- You can use the
int()
function to simply truncate the decimal part of the number. - You can use the
math.trunc()
method to truncate the decimal part of the number. - You can use the
math.floor()
method to round down to the nearest integer. - You can use the
math.ceil()
method to round up to the nearest integer.
It’s important to choose the appropriate method based on your specific use case and desired rounding behavior.