[英]How do I convert a number to a string in the same format as the input in python?
考慮以下幾行:
a = 5e7
print(str(a))
我得到 50000000.0 而不是 5e7 本身,我可以使用 str 命令本身來做到這一點嗎?
你也可以用另一種方式寫作。 我更喜歡使用這種方法,因為它更清晰。
a = 5e7
print(f"{a:0.0e}")
但是,如果在一個句子中要使用多個 int 或 float,則可以這樣寫。
print("{a:0.0e} ... {a:0.0e}".format(a = 5e7))
上面的代碼會給你 2 var "a"
更多代碼請查看
https://www.w3schools.com/python/ref_string_format.asp
希望這會幫助你。
無法知道原始輸入的格式,因為在解析過程中只記住了類型和值。
但是,如果您正在尋找如何以指數形式顯示數字,請使用e
格式說明符,如下所示:
a = 5e7
print("{:0.0e}".format(a))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.