Reputation: 5496
I have the following Python Decimal
variable:
mydecimal = Decimal('1.452300')
And the following resolution variable:
resolution = Decimal('0.0001')
How could I get the variable truncated to the resolution
value using just those two variables (which is the information I will have in my actual routine):
# Desired result:
truncated_mydecimal = Decimal('1.4523') # <-- How do I get this using 'resolution' and 'mydecimal' ?
Upvotes: 4
Views: 186
Reputation: 70582
This is what the quantize()
method is for:
>>> mydecimal.quantize(resolution)
Decimal('1.4523')
Read the docs, or do
>>> help(mydecimal.quantize)
for more. Caution: the precise value of your resolution
variable doesn't matter - it's the internal exponent quantize()
cares about. For example, nothing about the output above would change had we done:
>>> resolution = Decimal('0.0009')
instead.
Upvotes: 5