JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Converting Floating Point Numbers to Integers in Python

Author:JIYIK Last Updated:2025/05/07 Views:

Converting floating point numbers to integers in Python is relatively easy thanks to built-in functions and libraries. When converting floating point numbers to integers, there are two possibilities. Although it is easy to write your own function to accomplish this task, in this article we will only discuss how to use built-in functions and libraries.

Suppose we have a number, for example 1.52. If we wish to convert that number to an integer, we can use 2or 1. The former is the highest value and the latter is the lowest value. Since there are multiple functions to accomplish this task, they all perform the above task in different ways and return different values. So, choose the appropriate function based on your use case.


int()Convert floating point numbers to integers in Python using the

number = 25.49
print(int(number))

Output:

25

int()The function accepts an argument representing a number and converts it to an integer. This argument can be a string, a floating point value, or an integer itself. The function considers the integer value or the part before the decimal point in the number and returns it.

However, the behavior is slightly different when integer.9999999999999999passed as an argument in the form of an integer int(). If there are more than sixteen 9digits after the decimal point, the function will return in case of a positive number 整数+1and in case of a negative number 整数-1as the answer.

Refer to the following code snippet to understand the concept better.

print(int(1.5))
print(int(1.51))
print(int(1.49))
print(int(-1.5))
print(int(-1.51))
print(int(-1.49))
print(int(1.9999999999999999))
print(int(-1.9999999999999999))

Output:

0
1
1
1
1
-1
-1
-1
2
-2

mathConvert floating point numbers to integers using the Python module

We can use the built-in Python library mathto accomplish the same task. This library has various mathematical functions required for mathematical calculations.

We will discuss only maththree math functions in the library.

As the name suggests, trunc()the function truncates or cuts off the decimal part of the number passed as an argument and only considers the integer part. It int()behaves exactly like the built-in function, except for the exceptions we talked about above.

import math

print(math.trunc(0))
print(math.trunc(1))
print(math.trunc(1.5))
print(math.trunc(1.51))
print(math.trunc(1.49))
print(math.trunc(-1.5))
print(math.trunc(-1.51))
print(math.trunc(-1.49))
print(math.trunc(1.9999999999999999))
print(math.trunc(-1.9999999999999999))

Output:

0
1
1
1
1
-1
-1
-1
2
-2

See the official documentation to learn more about this function, here

Next, we have ceil()the function. This function returns the ceiling value of a number or the smallest integer greater than or equal to the number passed as an argument.

import math

print(math.ceil(0))
print(math.ceil(1))
print(math.ceil(1.5))
print(math.ceil(1.51))
print(math.ceil(1.49))
print(math.ceil(-1.5))
print(math.ceil(-1.51))
print(math.ceil(-1.49))
print(math.ceil(1.9999999999999999))
print(math.ceil(-1.9999999999999999))

Output:

0
1
2
2
2
-1
-1
-1
2
-2

See the official documentation to learn more about this function, here

Finally, we have floor()the function. This function returns the floor value of a number or the largest integer that is less than or equal to the number passed as an argument.

import math

print(math.floor(0))
print(math.floor(1))
print(math.floor(1.5))
print(math.floor(1.51))
print(math.floor(1.49))
print(math.floor(-1.5))
print(math.floor(-1.51))
print(math.floor(-1.49))
print(math.floor(1.9999999999999999))
print(math.floor(-1.9999999999999999))

Output:

0
1
1
1
1
-2
-2
-2
2
-2

See the official documentation to learn more about this function, here

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Implementing a Low-Pass Filter in Python

Publish Date:2025/05/07 Views:89 Category:Python

Low pass filter is a term in signal processing basics and is often used to filter signals to obtain more accurate results. This tutorial will discuss the low-pass filter and how to create and implement it in Python. A low-pass filter is use

Implementing Curl command in Python using requests module

Publish Date:2025/05/07 Views:97 Category:Python

requests This article will discuss and implement different curl commands using the module in Python . requests Installing modules in Python Python provides us with requests the module to execute curl command. Install it in Python 3 using Pi

Using fetchall() in Python to extract elements from a database

Publish Date:2025/05/07 Views:171 Category:Python

This article aims to describe fetchall() the working methods of extracting elements from a database using and how to display them correctly. This article will also discuss list(cursor) how functions can be used in programs. fetchall() Extra

Parsing log files in Python

Publish Date:2025/05/07 Views:106 Category:Python

Log files contain information about events that occurred during the operation of a software system or application. These events include errors, requests made by users, bugs, etc. Developers can further scan these usage details to find poten

Declaring a variable without a value in Python

Publish Date:2025/05/07 Views:57 Category:Python

A variable is a reserved memory location that can store some value. In other words, variables in a Python program provide data to the computer to process operations. Every value in Python has a data type. There are numbers, lists, tuples, e

Defining class global variables in Python

Publish Date:2025/05/07 Views:81 Category:Python

A global variable is a variable that is visible and available in every part of the program. Global variables are also not defined in any function or method. On the other hand, local variables are defined in functions and can be used only in

Incrementing loop step by 2 in Python

Publish Date:2025/05/07 Views:199 Category:Python

In each iteration, for the loop increases the counter variable by a constant. A loop with the sequence 0, 2, 4, 6 for will increase the counter variable by 2 each iteration. This article will show you some for ways to increment by 2 in a lo

Pool map with multiple parameters in Python

Publish Date:2025/05/07 Views:104 Category:Python

multiprocessing This article will explain different ways to perform parallel function execution using the module in Python . multiprocessing The module provides functionality to perform parallel function execution using multiple inputs and

Python if...else in Lambda function

Publish Date:2025/05/07 Views:68 Category:Python

lambda Functions are used to implement some simple logic in Python and can be thought of as anonymous functions. It can have multiple parameters but only one expression, just def like any other function defined using the keyword. We can def

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial