Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

5 Ways to handle precision values in Python

Handle Precision Values In Python

Hello, readers! In this article, we will be focusing on 5 Ways to handle precision values in Python. So, let us get started!


What do we mean by “handle precision values”?

Be it any programming language, we often come across situations, where we process integer or numeric data into any application or manipulation steps. In the same process, we find data with decimal values. This is when we need to handle precision values.

Python offers us various functions to handle precision values for integer data. It gives us a choice to exclude the decimal points or to have customized values depending upon the position of the decimal values in the number.

How to handle precision values in Python?

Let’s look at the different functions that Python offers for handling the precision data and decimals in our code outputs.

  1. Python % operator
  2. Python format() function
  3. Python round() function
  4. The trunc() function
  5. Math ceil() & floor() functions

Let us now go ahead and have a look at each one of them in the upcoming section.


1. Python % operator

With the ‘%’ operator, we can format the number as well as set precision limits to the same. By this, we can customize the limits of the precision points to be included in the resultant number.

Have a look at the below syntax!

Syntax:

'%.point'%number
  • point: It refers to the number of points we wish to have after the decimal in the number.
  • number: The integer value to be worked on.

2. Python format() function

Like the % operator, we can make use of the format() function too, which helps us set limits for the precision values. With the format() function, we format the data considering it a string and also set the limits for the points to be included after the decimal portion of the number.

Syntax:

print ("{0:.pointf}".format(number)) 

3. Python round() function

With Python round() function, we can extract and display the integer values in a customized format That is, we can select the number of digits to be displayed after the decimal point as a check for precision handling.

Syntax:

round(number, point)

Implementing Precision handling in Python

In the below example, we have utilized the above explained 3 functions to handle precision values in Python. We are here trying to handle precision by setting the digit to be displayed after the decimal point as 4.

num = 12.23456801

# using "%" operator
print ('%.4f'%num) 

# using format() function
print ("{0:.4f}".format(num)) 

# using round() function
print (round(num,4)) 

Output:

12.2346
12.2346
12.2346

Python math functions to handle precision

Apart from the above functions, python also offers us with math module that contains a set of functions to deal with the precision values.

Python math module has the below set of functions to deal with precision values–

  1. The trunc() function
  2. Python ceil() and floor() functions

Let us have a look at them one by one.


4. Python trunc() function

With trunc() function, all the digits after the decimal points gets terminated. That is, it returns the number preceding the decimal point only.

Have a look at the below syntax.

Syntax:

import math
math.trunc(number)

Example:

import math
num = 12.23456801

# using trunc() function
print (math.trunc(num)) 

Output:

12

5. Python ceil() and floor() function

With ceil() and floor() function, we can round off the decimal number to the closest high or low value.

The ceil() function rounds off the decimal number to the nearest large value after it. On the other hand, floor() function rounds off the value to the nearest low value before it.

Example:

import math
num = 12.23456801

# using ceil() function
print (math.ceil(num)) 

# using floor() function
print (math.floor(num)) 

Output:

13
12

Conclusion

Feel free to comment below, in case you come across any questions. For more such posts related to Python programming, stay tuned with us.

Till then, Happy Learning!! 🙂