![](/img/trans.png)
[英]How to retrieve Python format code from number represented as string?
[英]How to format a number represented as a string?
我有一個文本文件,其中有一個價格列,如 000245 和 001245。我想將字符串格式化為 2.45 和 12.45?
只需將其轉換為 int 並除以 100
int(price)/100
將價格轉換為字符串。
>>> price = "000245"
>>> int(price)/100
2.45
如果您使用字符串並想要返回字符串:
s = '000245'
s = s.lstrip('0')
print (s[:-2]+'.'+s[-2:])
Output(作為字符串):
2.45
否則,仍然要保留變量類型:
print (str(int(s)/100))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.