How to convert the float to Int in Python

How to convert the float to Int in Python

In this post we will give you information about How to convert the float to Int in Python. Hear we will give you detail about How to convert the float to Int in PythonAnd how to use it also give you demo for it if it is necessary.

we will learn about how to convert the float to an Int in Python with the help of examples.

Using int() method

To convert a float value to an Int, we can use the built-in int() method in Python.

The int() method takes the float value as an argument and converts it to integer.

Here is an example:

afloat = 12.79result = int(afloat)print(result)

Output:

12

Note: When we use the int() constructor the Integer is always rounded to the nearest downward value, like 12.752 to 12 or 6.99 to 6.

Using trunc() method

Similarly, we can also use the built-in trunc() method in python to convert a float to int.

The trunc() method takes the float value as an argument and returns the integer part of it by stripping the decimal values.

Here is an example:

from math import truncafloat = 12.79result = trunc(afloat)print(result)

Output:

12

Note: We have imported the trunc() method from the python math module

Hope this code and post will helped you for implement How to convert the float to Int in Python. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US