Passing a Decimal(str(value)) to a dictionary for raw value

2024/7/5 11:54:15

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.

Answer

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 probably Decimal(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.

https://en.xdnf.cn/q/119888.html

Related Q&A

Delete regex matching part of file

I have a file ,and i need to delete the regex matching part and write remaining lines to a file.Regex matching Code to delete file:import re with open("in1.txt") as f:lines = f.read()m = re.f…

How do I download files from the web using the requests module?

Im trying to download a webpage data to samplefile.txt on my hard drive using the following code:import requests res = requests.get(http://www.gutenberg.org/cache/epub/1112/pg1112.txt) res.raise_for_s…

how to get queryset from django orm create

i want to get queryset as a return value when i use the create in django ormnewUserTitle = User_Title.objects.none() newUserTitle = newUserQuestTitle | newUserReviewTitle newUserTitle = newUserQues…

Count and calculation in a 2D array in Python

I have 47 set of data to be analysised using Python with the following forma t and I stored the data in 2D array:2104,3,399900 1600,3,329900 2400,3,369000...I use len function to print the item stored …

How to create tree structure from hierarchical data in Python?

Hi I am a bit new to Python and am a bit confused how to proceed. I have a large dataset that contains both parent and child information. For example, if we have various items and their components, and…

%conda install -c conda-forge prophet takes forever to install

Trying to install fb prophet but it takes so long, it has been running for both vscode editor for almost an hour conda install -c conda-forge prophet

How to Send 2D array through php cURL

Im working with a a distributed system where a php app sends a post request to a python app. My code is pretty straight forward:$ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CU…

Internally, is asyncio run_forever() basically a while True loop?

python asyncio run_forever or while True is similar but it is a "should I do this..." question. I am more trying to understand if the internals of python asyncio is basically a while True:...…

How do I switch from python 2.6 to 2.7 by default

How do I switch from python 2.6 to 2.7 by defaultls -l /usr/bin/python* lrwxrwxrwx 1 root root 9 Jan 27 12:36 /usr/bin/python -> python2.6 lrwxrwxrwx 1 root root 9 Jan 27 12:36 /usr/bin/python…

How to make a flat list from nested lists? [duplicate]

This question already has answers here:Flatten an irregular (arbitrarily nested) list of lists(54 answers)Closed 1 year ago.Given a nested list of integers, implement an iterator to flatten it. Each el…