saltuklev
saltuklev

Reputation: 85

String slicing and manipulation

Price is coming like 48600.0 however the actual price is 4.86 USD so I am doing like

last_price3 = str(x["last_price_4d"]) #48600.0
last_price2 = last_price3[0:1] + "." + last_price3[:-4]
last_price = float(last_price2)

I can do like above but what if the price has more than 1 numbers in front of the dot like 176.85 USD last_price_4d will be like 1768500.0

Thank you

Upvotes: 3

Views: 74

Answers (2)

tc5368
tc5368

Reputation: 11

If the number is scaled for being 10,000 times larger you can just divide but if you did want to do it with just string manipulation you can do

last_price3 = str(x["last_price_4d"])
i = last_price3.index('.')
last_price = last_price3[:i-2][:-2]+'.'+last_price3[:i-2][-2:]

Upvotes: 1

Olivier Melançon
Olivier Melançon

Reputation: 22294

What about dividing the price by 10,000 since that is how it has been scaled?

last_price = round(x["last_price_4d"] / 10000, 2)

Upvotes: 4

Related Questions