×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Precision handling in Python

By IncludeHelp Last updated : December 08, 2024

Handling precisions

There are many ways to handle/set the precision with a float value, some of the math functions and some of them are based on the formatting.

Example without handling precision

# Python program to print float value
import math

a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3

print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)

Output

Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335

Methods to handle the precision in Python

Below are the methods which can be used to handle precisions in Python:

1. Using the trunc() method

The trunc() method is used to get the truncated integer value of a number, it accepts a number (either an integer or a float) and returns the real value truncated to an integral.

Example

# Importing math module
import math

a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3

print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)

# using trunc() method
print("Using trunc() method...")
print("math.trunc(a): ", math.trunc(a))
print("math.trunc(b): ", math.trunc(b))
print("math.trunc(c): ", math.trunc(c))
print("math.trunc(d): ", math.trunc(d))

The output of the above example is:

Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using trunc() method...
math.trunc(a):  10
math.trunc(b):  10
math.trunc(c):  10
math.trunc(d):  3

2. Using the ceil() method

The ceil() method is used to get the ceil value of a given number, it accepts a number/numeric expression and returns the smallest integral value which is greater than the number.

Example

# Importing math module
import math

a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3

print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)

# using ceil() method
print("Using ceil() method...")
print("math.ceil(a): ", math.ceil(a))
print("math.ceil(b): ", math.ceil(b))
print("math.ceil(c): ", math.ceil(c))
print("math.ceil(d): ", math.ceil(d))

The output of the above example is:

Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using ceil() method...
math.ceil(a):  11
math.ceil(b):  10
math.ceil(c):  11
math.ceil(d):  4

3. Using the floor() method

The floor() method is used to get the floor value of a given number, it is used to get the floor value of a number, it accepts a number/numeric expression and returns largest integer (integral) value, which is not greater than the number.

Example

# Importing math module
import math

a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3

print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)

# using floor() method
print("Using floor() method...")
print("math.floor(a): ", math.floor(a))
print("math.floor(b): ", math.floor(b))
print("math.floor(c): ", math.floor(c))
print("math.floor(d): ", math.floor(d))

The output of the above example is:

Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using floor() method...
math.floor(a):  10
math.floor(b):  10
math.floor(c):  10
math.floor(d):  3

Handing precision using different ways of formatting in Python

Below are the different formatting techniques that can be used to handle precisions in Python:

1. Using "%" and format specifier (%f)

This method is similar to printf() function in C language, it is used to format as well as set the precision.

Example

a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3

print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)

print("Using %...")
print("%.2f" % a)
print("%.2f" % b)
print("%.2f" % c)
print("%.2f" % d)

The output of the above example is:

a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using %...
10.12
10.00
10.13
3.33

2. Using format() method

The format() method is used to format a string and we can define the number of precision.

Example

a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3

print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)

print("Using format()...")
print("{0:.2f}".format(a))
print("{0:.2f}".format(b))
print("{0:.2f}".format(c))
print("{0:.2f}".format(d))

The output of the above example is:

Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using format()...
10.12
10.00
10.13
3.33

3. Using round() method

The round() method rounds the float value with given number of precisions.

Example

a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3

print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)

print("Using round()...")
print(round(a, 2))
print(round(b, 2))
print(round(c, 2))
print(round(d, 2))

The output of the above example is:

Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using round()...
10.12
10.0
10.13
3.33

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.