1

I am trying to use the Decimal module to do some FX calculations (instead of using floats).

However when I do the following, I do not get the expected value output:

>>> from decimal import Decimal
>>> x = Decimal(1.3755)
>>> y = Decimal(1.2627)
>>> z = y/(1/x)
>>> print(z)
1.736843849999999839084452447
>>>

The output should be: 1.73684385

I thought using Decimals would correct this issue. How can I resolve this rounding issue?

3
  • use round , round(z, 5) Commented Sep 1, 2021 at 9:32
  • I don't want to have to round, I just want to get the full output 1.73684385 without having to guess what to round to, in this case I'd need round(z, 8) Commented Sep 1, 2021 at 9:34
  • set the getcontext().prec which will gives what you excected Commented Sep 1, 2021 at 9:38

4 Answers 4

4
from decimal import Decimal
x = Decimal('1.3755')
y = Decimal('1.2627')
print(y/(1/x))
# 1.736843850000000000000000000

Use str to construct Decimal instead of real number.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that there's still a certain amount of luck involved here - you need the roundings to go the right way. With these particular values this "works", but if you try with x = Decimal('5.73') and y = Decimal('6.7'), then print(y/(1/x)) gives 38.39099999999999999999999999 rather than the expected 38.39100000000000000000000000.
2

String decimal objects are required:

>>> from decimal import Decimal
>>> x = Decimal('1.3755')
>>> y = Decimal('1.2627')
>>> z = y/(1/x)
>>> print(z)
1.736843850000000000000000000
>>>

Otherwise they would still be treated as regular python floats.

As @schwobaseggl mentioned:

Note that the float is passed to the Decimal constructor, so the precision is lost there already. The Decimal constructor has no chance of knowing the desired precision. The result is still a decimal object, but with the precision of the passed float.

2 Comments

Note that the float is passed to the Decimal constructor, so the precision is lost there already. The Decimal constructor has no chance of knowing the desired precision. The result is still a decimal object, but with the precision of the passed float.
@schwobaseggl Yeap, I will edit that in! thanks man
1

you can use 0:.nf to round the numbers like below :

print(z)
# 1.736843849999999839084452447
print("{0:.8f}".format(z))
# 1.73684385 

2 Comments

This does not explain at all why the issue occurs with Decimal objects who when handled with intent deal with the precision themselves. This question is not about string formatting.
Didn't downvote, but how do you know how many relevant the digit the result has?
0

try this, this will helps you, set your precision level

>>> from decimal import *
>>> getcontext().prec = 6
>>> Decimal(1) / Decimal(7)
Decimal('0.142857')
>>> getcontext().prec = 28
>>> Decimal(1) / Decimal(7)
Decimal('0.1428571428571428571428571429')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.