【问题标题】:Displaying amount - Python [duplicate]显示金额 - Python [重复]
【发布时间】:2015-02-28 13:33:55
【问题描述】:
print("The cost of paint based on whole gallons is: $", round(paint_costs,2))
The cost of paint based on whole gallons is: $ XXX.XX

如何省略 $ 和金额之间的空格,使其显示为:

The cost of paint based on whole gallons is: $XXX.XX

import.locale吗?

【问题讨论】:

  • 在您的情况下,使用字符串格式是正确的答案:"The cost of paint based on whole gallons is: ${0:.2f}".format(paint_costs)。不要使用round 来格式化数字。
  • 如果您认为解决方案已经解决了您的问题,请将其标记为已接受。见meta.stackexchange.com/questions/5234/…

标签: python printing currency


【解决方案1】:

在打印函数中添加sep=""参数。

print("The cost of paint based on whole gallons is: $", round(paint_costs,2), sep="")

【讨论】:

    【解决方案2】:

    使用 Py3 功能sep

    print("The cost of paint based on whole gallons is: $", round(paint_costs,2),sep = "")
    

    其他方式包括

    • print("The cost of paint based on whole gallons is: ${}".format(round(paint_costs,2)))
    • print("The cost of paint based on whole gallons is: $%s"%(round(paint_costs,2)))

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 2023-04-01
      • 2023-03-31
      • 2010-10-14
      • 2017-07-10
      • 2019-02-28
      • 2014-07-29
      • 1970-01-01
      • 2013-12-06
      相关资源
      最近更新 更多