2017-07-11 84 views

回答

3

可以使用strip方法:

x = 0.13 
res = str(x).strip('0') 
print('This is my formatted number: {}'.format(res)) 
+0

假定x是一個浮點數,我想把它截斷到小數點後兩位,但除去前導零。有沒有一種簡單的方法來做到這一點在python 3? –

+0

是的,你可以使用'round'函數:'''res = str(round(x,2))。strip('0')''' –

相關問題