Home > Enterprise >  Python3 Json Dictionary output price print max with 2 decimal place
Python3 Json Dictionary output price print max with 2 decimal place

Time:11-01

I managed final out put from a json dictionary (Price of an item)

from the output price list , I want to find the max number not grater then 1.90 with 2 decimal place

in the list we have 45.86834862385321 but my final output must print as 1.24

resp2json = rr.json()
a = (resp2json['data']['pencil'])

for b in a:
    final_data = b ['blue'] ['rate']
    print (final_data)

OUTPUT

1.2157339821573399
1.2413108242303872
1.2157339821573399
45.86834862385321
1.2165314401622718

CodePudding user response:

try this one:

data=[
1.2157339821573399,
1.2413108242303872,
1.2157339821573399,
45.86834862385321,
1.2165314401622718,
]
temp = 0 
for row in data:
    if temp < row < 1.9:
        temp = row
print(round(float(temp),2))

temp < row < 1.9 this is the logic you mentioned in your question

CodePudding user response:

I think that this code will return the output with 2 decimal place.

resp2json = rr.json()
a = (resp2json['data']['pencil'])

for b in a:
    final_data = b ['blue'] ['rate']
    print (f'{final_data:.2f}')
  • Related