I'm needing to pass values to a dictionary as class 'decimal.Decimal', and the following keeps happening:
from decimal import *
transaction_amount = 100.03
transaction_amount = Decimal(str(transaction_amount))
item = {
'transaction_amount': transaction_amount
}
print(item)
Results:
{'transaction_amount': Decimal('100.03')}
How do I attain the raw 100.03 result, rather than Decimal('100.03')?
This is what I want the dictionary to have saved:
{'transaction_amount': 100.03)}
When I do:
print(transaction_amount)
The result is as expected:
100.03
So where am I going wrong?
I don't see any reason why this question should be downvoted.
When you ask python to print an object, it'll print the textual representation of that object.
Here your variable item is a dictionnary, so its representation is as follow:
{ key: value [, repeat]}
If you want the value inside your dictionnary, you would have to go at the specified key like so :
print(item[transaction_amount])
So basically, your code was fine and your use of decimal too, but you weren't testing it the good way. ;) Happens a lot.
Edit: It's worth noting that, since what you are getting within the dictionnary object is
Decimal(100.03)
, even when printing the value of the key-value pair as I showed previously, you won't get a pure 100.03, but probablyDecimal(100.03)
.I'll search some documentation as to how to get the string. Welp apparently no, it should work like a charm.
Edit: I didn't get the question (which has been edited since then) right.
However because of the extended conversation in the comment section, it'll remain here.