Tutorials  /  Python

Python Decimal: Precision Handling in Python

Ccentron Redaktion · February 2024 ·5 min read ·Python, Tutorial

In this lesson on decimal module in Python, we will see how we can manage decimal numbers in our programs for precision and formatting and making calculations as well. The precision with decimal numbers is very easy to lose if numbers are not handled correctly. Let’s see how the decimal module and its available functions help in these areas.

Working with Decimal Numbers

Decimal numbers are just the floating-point numbers with fixed decimal points. We must round off the numbers correctly based on our need, otherwise, the results can be unexpected. Python’s decimal module helps us to be more precise with decimal numbers.

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Need for Decimal Module

Before actually putting this module to use, let’s see what precision are we talking about and establish why we need this module actually. Look at the following code snippet:

Python
division = 72 / 7
print(division)

Let’s see the output for this program:

Code
sh-3.2$ python decimal_module_need.py
10

Using the Decimal Module

In all the programs we make in this post, we will be importing the decimal module in all of them:

Python
import decimal

It might be the case that we only import a specific function from this module. This can be done as:

Python
from decimal import Decimal

Let’s start putting the decimal module into some programs.

Python Decimal Module Example

We will start with examples related to the module now.

Correcting Division with Decimals

Here, we will correct the program we wrote above to perform division which should have produced a floating-point result. Modified program with the decimal module will look like:

Python
import decimal

division = decimal.Decimal(72) / decimal.Decimal(7)
print(division)

Let’s see the output for this program:

Code
sh-3.2$ python python_correct_decimal_division.py
10.28571328571428571428571429
sh-3.2$

To notice, the division is correct now and precise as well, or maybe it is too precise?

Controlling Precision for Single Operation

In the last program, there were 25 decimal places in the answer now. But what if we only wanted up to three places of decimal values? This can be controlled as well. Here, we will control the precision of the answer which will not reflect in other operations in our program:

Python
import decimal

with decimal.localcontext() as ctx:
    ctx.prec = 3
    division = decimal.Decimal(72) / decimal.Decimal(7)
    print(division)

again = decimal.Decimal(72) / decimal.Decimal(7)
print(again)

We did the division operation two times to prove a point. Let’s see the output for this program:

Code
sh-3.2$ python_decimal_division_precision.py
10.3
10.28571328571428571428571429
sh-3.2$

Noticed something? The precision we set was only valid for a single time. Next time we did the division, we got back the same result.

Controlling Precision for Complete Program

It is also possible to control precision globally in the program. This is not recommended all the time when you are dealing with a lot of numbers in your program. Here is an example:

Python
import decimal

decimal.getcontext().prec = 3

division = decimal.Decimal(72) / decimal.Decimal(7)
print(division)

again = decimal.Decimal(72) / decimal.Decimal(7)
print(again)

Let’s see the output for this program:

Code
sh-3.2$ python_decimal_division_precision_global.py
10.3
10.3
sh-3.2$

Rounding the Numbers

It is possible to gracefully round the numbers with the round(...) function. Let’s try it:

Python
import decimal

#Can be rounded to 13.48 or 13.49
rounded = round(13.485, 2)
print(rounded)

Let’s see the output for this program:

Code
sh-3.2$ python_round.py
13.48
sh-3.2$

The number in program can be rounded to 13.48 or 13.49. By default, the round(...) function rounds down. This can be changed as well:

Python
import decimal

#Can be rounded to 13.48 or 13.49
rounded = round(13.485, 2)
print(decimal.Decimal(rounded).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_UP))

Let’s see the output for this program:

Code
sh-3.2$ python_round_up.py
13.49
sh-3.2$

Getting Python Decimal Context

If you are interested looking at default context set by default for decimal module, you can use the following script:

Python
from decimal import *
print(getcontext())

Let’s see the output for this program:

Code
sh-3.2$ python python_decimal_context.py
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, ca
pitals=1, flags=[], traps=[DivisionByZero, Overflow, InvalidOperation])
sh-3.2$

Conclusion

That’s all for python decimal module, it’s very useful when working with float point numbers. Python Decimal: Precision Handling in Python

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren