 # How to convert Float to Int in Python?

## How to convert Float to Int in Python?

Converting a 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
```

Method 1: Conversion using int():

To convert a float value to int we make use of the built-in int() function, this function trims the values after the decimal point and returns only the integer/whole number part.

Syntax: int(x)

Return: integer value

Example 1: Number of type float is converted to a result of type int.

 `# conversion from float to int` ` ` `num ``=` `9.3` ` ` `# printing data type of 'num' ` `print``(``'type:'``, ` `      ``type``(num).__name__)  ` ` ` `# conversion to int` `num ``=` `int``(num)   ` ` ` `# printing data type of 'num' ` `print``(``'converted value:'``, num, ` `      ``', type:'``, ``type``(num).__name__)`

Output:

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

Example 2: 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.

 `# example of unpredictable ` `# behaviour of int()` ` ` `num1 ``=` `5.9` `num2 ``=` `5.99999999999999999999` ` ` `num1 ``=` `int``(num1)` `num2 ``=` `int``(num2)` ` ` `print``(num1, num2, sep ``=` `'\n'``)`

Output:

```5
6```

Method 2: Conversion using math.floor() and math.ceil().

A float value can be converted to an int value no larger than the input 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. The math module is to be imported in order to use these methods.

Syntax: math.floor(x)

Parameter:

x: This is a numeric expression.

Returns: largest integer not greater than x.

Syntax: math.ceil(x)

Parameter:

x: This is a numeric expression.

Returns: Smallest integer not less than x.

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

 `# conversion using floor and ceil .` ` ` `# importing math module` `import` `math       ` ` ` `num ``=` `5.6` ` ` `floor_value ``=` `math.floor(num)` ` ` `ceil_value  ``=` `math.ceil(num)` ` ` `print``(``"the result using floor() : "``,` `      ``floor_value , ` `      ``', type : '``,``type``(floor_value).__name__)` ` ` `print``(``"the result using ceil()  : "``,` `      ``ceil_value,` `      ``', type: '``, ``type``(ceil_value).__name__)`

Output:

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

Last Updated on October 24, 2021 by admin

## Pandas DataFrame.ix[ ]Pandas DataFrame.ix[ ]

Pandas DataFrame.ix[ ] Pandas DataFrame.ix[ ] is both Label and Integer based slicing technique. Besides pure label based and integer based, Pandas provides a hybrid method for selections and subsetting the object using the ix[] operator. ix[] is the most

## Python – Pandas Series.str.replace() to replace text in a seriesPython – Pandas Series.str.replace() to replace text in a series

Pandas Series.str.replace() to replace text in a series   Pandas Series.str.replace() method works like Python .replace() method only, but it works on Series too. Before calling .replace() on a Pandas series, .str has to be prefixed in order to differentiate

## Plot multiple plots in MatplotlibPlot multiple plots in Matplotlib

Plot multiple plots in Matplotlib In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear

## Convert JSON to dictionary in PythonConvert JSON to dictionary in Python

Convert JSON to dictionary in Python JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through

## Increment and Decrement Operators in PythonIncrement and Decrement Operators in Python

Increment and Decrement Operators in Python If you’re familiar with Python, you would have known Increment and Decrement operators ( both pre and post) are not allowed in it. Python is designed to be consistent and readable. One common error

## keras.fit() and keras.fit_generator()keras.fit() and keras.fit_generator()

keras.fit() and keras.fit_generator() keras.fit() and keras.fit_generator() in Python are two separate deep learning libraries which can be used to train our machine learning and deep learning models. Both these functions can do the same task, but when to use which function

## Python Regex: re.search() VS re.findall()Python Regex: re.search() VS re.findall()

Python Regex: re.search() VS re.findall() A Regular expression (sometimes called a Rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations. Regular expressions are

## Python – shutil.copy() methodPython – shutil.copy() method

Python | shutil.copy() method # Python program to explain shutil.copy() method    # importing shutil module import shutil # Source path source = "/home/User/Documents/file.txt" # Destination path destination = "/home/User/Documents/file.txt" # Copy the content of # source to destination try: