Open In App
Related Articles
• Write an Interview Experience
• Machine Learning Algorithms
• Precision Handling in Python

Precision Handling in Python

Python in its definition allows handling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the “math” module. In this article, we will use high-precision calculations in Python with Decimal in Python.

Example

`Input: x = 2.4Output: Integral value of no = 2        Smallest integer value = 2        Greatest integer value = 3Explanation: In this, we are handling precision values in python.`

Python Ways to Precision Handling

we will explore the various functions that Python provides for managing precise data and decimal points.

Python Handle Precision using Rounding Method

Python provides for managing precise data and decimal points, the round`()` function is used to round a number to a specified number of decimal places. Alternatively, string formatting options, such as the f-string syntax or the `format()` method, allow you to format numbers with precise decimal places.

Python3

 `rounded ``=` `round``(``8.88888``, ``2``)``print``(rounded)`` ` `formatted ``=` `"{:.3f}"``.``format``(``9.999``)``print``(formatted)`

Output

`8.899.999`

Time Complexity: O(1)
Auxiliary Space: O(1)

Python Handle Precision using Getcontext()

In Python, we can handle precision values using getcontext(). The `getcontext()` function is used to access the current decimal context, which provides control over precision settings. The precision is used to set the desired number of decimal places for mathematical operations using the decimal class.

Python3

 `from` `decimal ``import` `Decimal, getcontext`` ` `getcontext().prec ``=` `3`` ` `result ``=` `Decimal(``'3'``) ``/` `Decimal(``'9'``)``print``(result)`

Output

`0.333`

Time Complexity: O(1)
Auxiliary Space: O(1)

Python Handle Precision with Math Module

In Python, we can handle precision values using Math Module. In this example, we will cover some Python math methods. such as trunc() method, ceil() method, and floor() method.

• trunc() – This function is used to eliminate all decimal parts of the floating-point number and return the integer without the decimal part.
• ceil() – This function is used to print the least integer greater than the given number.
• floor() – This function is used to print the greatest integer smaller than the given integer.

Python3

 `import` `math`` ` `a ``=` `3.4536`` ` `print``(``"The integral value of number is : "``, end``=``"")``print``(math.trunc(a))`` ` `# using ceil() to print number after ceiling``print``(``"The smallest integer greater than number is : "``, end``=``"")``print``(math.ceil(a))`` ` `# using floor() to print number after flooring``print``(``"The greatest integer smaller than number is : "``, end``=``"")``print``(math.floor(a))`

Output

`The integral value of number is : 3The smallest integer greater than number is : 4The greatest integer smaller than number is : 3`

Time Complexity: O(1)
Auxiliary Space: O(1)

Python Handle Precision with Decimal Module

In Python, we can handle precision values using Decimal Module. In this example, we will see How to Limit Float to Two Decimal Points in Python. In Python float precision to 2 floats in Python, and in Python float precision to 3. There are many ways to set the precision of the floating-point values. Some of them are discussed below.

• Using % – The % operator is used to format as well as set precision in Python. This is similar to “printf” statement in C programming.
• Using format() – This is yet another way to format the string for setting precision.
• Using round(x,n) – This function takes 2 arguments, number, and the number till which we want the decimal part rounded.
• Using f-string – PEP 498 introduced a new string formatting mechanism known as Literal String Interpolation or more commonly as F-strings

Python3

 `a ``=` `3.4536`` ` `print``(``"The value of number till 2 decimal place(using %) is : "``, end``=``"")``print``(``'%.2f'` `%` `a)`` ` `# using format() to print value till 3 decimal places``print``(``"The value of number till 3 decimal place(using format()) is : "``, end``=``"")``print``(``"{0:.3f}"``.``format``(a))`` ` `# using round() to print value till 2 decimal places``print``(``"The value of number till 2 decimal place(using round()) is : "``, end``=``"")``print``(``round``(a, ``2``))`` ` `# using f-string to print value till 2 decimal places``print``(``"The value of number till 2 decimal place(using f-string) is : "``, end``=``"")``print``(f``"{a:0,.2f}"``)`

Output

`The value of number till 2 decimal place(using %) is : 3.45The value of number till 3 decimal place(using format()) is : 3.454The value of number till 2 decimal place(using round()) is : 3.45The value of number till 2 decimal place(using f-string) is : 3.45`

Time Complexity: O(1)
Auxiliary Space: O(1)