簡體   English   中英

字符串切片和操作

[英]String slicing and manipulation

價格接近48600.0然而實際價格是4.86美元,所以我喜歡

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

我可以像上面那樣做,但是如果價格在點前有多於1個數字,如176.85 USD,last_price_4d將會是1768500.0

謝謝

那么將價格除以10,000,那么它是如何縮放的呢?

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

如果數字的縮放比例大10,000倍,那么你可以分開,但如果你確實想要只用字符串操作來做,你可以做

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM