[英]Why does using float() remove my formatted decimal places?
我正在嘗試編寫一個比較運費的簡單程序。 我有一個默認的浮動價值,它是溢價和兩個檢查它的功能,並根據他們的產品重量為用戶提供最便宜的價值。
我的代碼如下:
premium_shipping = 125.00
def ground_shipping(weight):
if weight <= 2.0 and weight >= 0:
return float('{:.2f}'.format((weight * 1.50) + 20))
elif weight > 2.0 and weight <= 6.0:
return float('{:.2f}'.format((weight * 3.00) + 20))
elif weight > 6.0 and weight <= 10.0:
return float('{:.2f}'.format((weight * 4.00) + 20))
elif weight > 10:
return float('{:.2f}'.format((weight * 4.75) + 20))
else:
return "Your package doesn't weigh anything!"
def drone_shipping(weight):
if weight <= 2.0 and weight >= 0:
return float('{:.2f}'.format(weight * 4.50))
elif weight > 2.0 and weight <= 6.0:
return float('{:.2f}'.format(weight * 9.00))
elif weight > 6.0 and weight <= 10.0:
return float('{:.2f}'.format(weight * 12.00))
elif weight > 10:
return float('{:.2f}'.format(weight * 14.25))
else:
return "Your package doesn't weigh anything!"
def cheapest_shipping(weight):
if ground_shipping(weight) < drone_shipping(weight) and ground_shipping(weight) < premium_shipping:
return f'The cheapest shipping method is ground shipping. It would cost {ground_shipping(weight)} to ship your item.'
elif drone_shipping(weight) < ground_shipping(weight) and drone_shipping(weight) < premium_shipping:
return f'The cheapest shipping method is drone shipping. It would cost {drone_shipping(weight)} to ship your item.'
elif premium_shipping < ground_shipping(weight) and premium_shipping < drone_shipping(weight):
return f'The cheapest shipping method is premium shipping. It would cost {premium_shipping} to ship your item.'
else:
return "Error. You have input an invalid weight."
print(ground_shipping(4.8))
# 34.4
print(cheapest_shipping(4.8))
# The cheapest shipping method is ground shipping. It would cost 34.4 to ship your item.
print(cheapest_shipping(41.5))
當我這樣做時,我在技術上得到了我的答案,但是我希望它在小數點后 2 位當我從兩個函數中刪除float()時,我得到的值是小數點后 2 位,但是是一個 str. 當我包含float()時,它會將我的數字返回為帶 1 個小數位的浮點數,我不確定如何將其更改為包含 2 個小數點。
提前致謝!
float
值2
、 2.0
、 2.00000000000000000
和200e-2
之間沒有區別。 如果你想以某種方式呈現float
,最好在格式化時完成,使用以下任何一種(按優先順序):
>>> pi = 3.14159
>>> f"{pi:.2f}" # f-strings, introduced in Python 3.6.
'3.14'
>>> "{:.2f}".format(pi) # str.format, use f-strings in preference.
'3.14'
>>> "%.2f" % (pi) # VERY old method, avoid if possible.
'3.14'
如果您使用的是 Python 的最新版本,f 字符串就是通往 go 的方式。與%
和str.format()
不同,它們將數據項本地化到將在字符串中打印的位置,因此您不必必須 go 在參數列表中搜索它。 對比以下內容:
f"Hello, {name}, today is {dayOfWeek}"
"Hello, {}, today is {}".format(name, dayOfWeek)
您似乎對float
是什么以及它的顯示方式有些困惑。 float
是具有 53 位十進制精度的二進制數,可以容納多種值。 您可以根據需要顯示float
。 例如:
float('{:.2f}'.format(weight * 14.25))
這里你取一個float
值weight * 14.25
,使用format
方法將其轉換為具有兩位小數的字符串,然后返回float
function。這可能會也可能不會截斷小數點后第二位的數字,因為大多數小數不能完全用二進制表示。
您顯示您的價值觀時沒有同樣的考慮,但是:
print(ground_shipping(4.8))
如果你想把它打印成兩位數,你應該像以前一樣格式化它:
print(f'{ground_shipping(4.8):0.2f}')
要么
print('{:0.2f}'.format(ground_shipping(4.8)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.