为什么使用 float() 会删除我的格式化小数位?

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() 时,我得到的值是小数点后两位,但它是一个 str。当我包含 float() 时,它 returns 我的数字作为带 1 位小数的浮点数,我不确定如何将其更改为包含 2 位小数。

提前致谢!

float22.02.00000000000000000和[=16=之间没有区别].如果您想以某种方式 呈现 一个 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-strings 是可行的方法。与 %str.format() 不同,它们将数据项本地化到它将在字符串中打印的位置,因此您不必在参数列表中搜索它。对比如下:

f"Hello, {name}, today is {dayOfWeek}"
"Hello, {}, today is {}".format(name, dayOfWeek)

您似乎对 float 是什么以及它的显示方式有些混淆。 float 是一个具有 53 位十进制精度的二进制数,可以容纳多种值。您可以根据需要显示 float。例如:

 float('{:.2f}'.format(weight * 14.25))

这里取一个floatweight * 14.25,使用format方法将其转换为小数点后两位的字符串,然后用float函数返回.这可能会或可能不会截断小数点后第二位的数字,因为大多数小数不能用二进制精确表示。

你显示你的价值观没有同样的考虑,但是:

print(ground_shipping(4.8))

如果您想将其打印为 two-digit 数字,您应该按照与之前相同的方式对其进行格式化:

print(f'{ground_shipping(4.8):0.2f}')

print('{:0.2f}'.format(ground_shipping(4.8)))