filmov
tv
Python Tutorial: Python Decimal Module - Python Numbers #27

Показать описание
Python Decimal Module
In the previous two Python tutorials, we looked at issues that arise when working collaboratively with floating point numbers in the Python programming language. When working with floating point numbers we have seen that Python sometimes returns a value that we are not suspecting. This is doesn't happen often but when we get a number that is a few ten thousandths off this can be concerning especially if you are working with numbers that need to have precise returns like a accounting program or a program that controls trajectory of a spacecraft. It would be concerning if we could not get the correct amount of money or if our spacecraft was off course by a tad bit we would probably miss the earth on the return and fly right into the sun.
In this tutorial, we are going to look at the Python decimal module which needs to import into your program for the functions to work. Decimals are basically floating point numbers but, they have fixed number of decimal points. We can think of them as fixed-precision floating point numbers. We can also control how Python rounds the numbers unlike with the round() function where we saw if can round in the wrong direction. With the decimal module, we will be capable of more precise numbers with a little bit more work.
Importing The Decimal Module
We have seen how to import a few modules in previous tutorials, in this tutorial we are going to follow the same steps. To make the decimal module to work we first need to import the module and we do this by entering the following "import decimal" with no quotes. Once we have imported the Python decimal module we will be able to access the functions included in the module.
Комментарии