FAQ: Modules: Python - Modules Python Decimals

Maybe I’m missing the point of this lesson, but the solution doesn’t output what I would expect. Why isn’t the below more correct?

# Import Decimal below:
from decimal import Decimal

# Fix the floating point math below:
two_decimal_points = round(Decimal(0.2 +0.69), 2)
print(two_decimal_points)

four_decimal_points = round(Decimal(0.53 * 0.65), 4)
print(four_decimal_points)

1 Like

The idea would seem to be, operate on the Decimal objects.

three_decimal_points = Decimal('0.2') * Decimal('0.69')
print(three_decimal_points)

four_decimal_points = Decimal('0.53') * Decimal('0.65')
print(four_decimal_points)
0.138
0.3445

Note how we gave string objects to Decimal.

Thanks for your response. I wasn’t making the numbers into strings using the pure Decimal way, so I was getting a much longer number.

2 Likes

You’re welcome. These objects definitely require some in depth study in order to gain a full understanding of their usage, especially optimal usage. SigDigs is one area that is worthy of consideration. Decimal objects do not require a round() function since they have a precision attribute. It’s been a long while since I first tried to explore this class. In my limited clime there were not a lot of use cases and it fell to the wayside (but not forgotten).

Explore the class and bring forward any other observations or use cases that you envision.

SERP

what are possible use cases of the Decimal class py - Google Search

2 Likes

Why do we need to convert the integers to string in order for Decimal to work? And why does Python return those weirdly formatted numbers without decimal in the first place?

Because the goal of Decimal to create numbers and do math that more closely resembles how humans think through operations it is important to distinguish it from ordinary integers, and more importantly from ordinary floats. The only way to do this is to make each object a string, and let the decimal module sort things out.

Why use decimal instead of the built_in function round?

round is a general purpose function that does not take into account significant figures (sigdigs). Of course there is much more to it than that. Decimal objects are strings, rather than floats, which may contribute to faster processing (needs to be checked).

How does it know how many decimal points to do? It’s not because it’s written. This course is becoming worse and worse with each module. Why charge us so much money for a wiki entry on what the module is about? I learned nothing here from the course content.

From an amateur point of view, I found and still do find the Decimal module a little more than confusing. However, I expect that judicious use of the module might well fit an environment where decimal constraints are strictly conformed with, such as significant figures in science calculations. Bottom line, this is just one blip that most learners experience. Keep going. It’s how you will get your money’s worth as long as you see this as an investment in yourself.