2
我有三個花車,我想輸出爲2位小數位字符串。Python如何格式化貨幣字符串
amount1 = 0.1
amount2 = 0.0
amount3 = 1.87
我想將它們全部輸出爲分別類似於0.10,0.00和1.87的字符串。
我該如何有效地做到這一點?
我有三個花車,我想輸出爲2位小數位字符串。Python如何格式化貨幣字符串
amount1 = 0.1
amount2 = 0.0
amount3 = 1.87
我想將它們全部輸出爲分別類似於0.10,0.00和1.87的字符串。
我該如何有效地做到這一點?
另一種直接格式化他們是locale STDLIB模塊
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'en_US.utf8'
>>> locale.currency(123.2342343234234234)
'$123.23'
>>> locale.currency(123.2342343234234234, '') # the second argument controls the symbol
'123.23'
這是好的,因爲你可以設置區域設置爲用戶的默認爲我做的,然後打印在他們的約定。