0
假設我有一個我想放入字符串的數字。如何格式化字符串以刪除前導零?
x = 0.13
如何格式化我的字符串,以便不會出現前導零?
print('This is my formatted number: {formating}'.format(x))
>> This is my formatted number: .13
假設我有一個我想放入字符串的數字。如何格式化字符串以刪除前導零?
x = 0.13
如何格式化我的字符串,以便不會出現前導零?
print('This is my formatted number: {formating}'.format(x))
>> This is my formatted number: .13
可以使用strip
方法:
x = 0.13
res = str(x).strip('0')
print('This is my formatted number: {}'.format(res))
假定x是一個浮點數,我想把它截斷到小數點後兩位,但除去前導零。有沒有一種簡單的方法來做到這一點在python 3? –
是的,你可以使用'round'函數:'''res = str(round(x,2))。strip('0')''' –