Hướng dẫn python division decimal precision
Python decimal module helps us in division with proper precision and rounding of numbers. Show Nội dung chính
Python decimal moduleIn 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 numbersDecimal 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. Need for decimal moduleBefore 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:
Let’s see the output for this program: Using the decimal moduleIn all the programs we make in this post, we will be importing the decimal module in all of them:
It might be the case that we only import a specific function from this module. This can be done as:
Let’s start putting the decimal module into some programs. Python decimal module exampleWe will start with examples related to the module now. Correcting Division with decimalsHere, 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:
Let’s see the output for this program: Controllling Precision for single operationIn 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:
We did the division operation two times to prove a point. Let’s see the output for this program: 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.Controllling Precision for complete programIt 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:
Let’s see the output for this program: Rounding the numbersIt is possible to
gracefully round the numbers with the
Let’s see the output for this program: The number in program can be rounded to 13.48 or 13.49. By default, theround(...) function rounds down. This can be changed as well:
Let’s see the output for this program: Getting Python Decimal ContextIf you are interested looking at default context set by default for decimal module, you can use the following script:
Let’s see the output for this program: That’s all for python decimal module, it’s very useful when working with float point numbers. |