【问题标题】:How do I get my numbers in python to show up with $?如何让我在 python 中的数字显示为 $?
【发布时间】:2021-08-30 11:28:47
【问题描述】:

我在读高中,我是第一次尝试 python。我希望我的方程式在数字前用 $ 打印。这是我现在的代码:

revenue = 98456
costs = 45000
profit = revenue - costs
print(profit)

我的教授说你可以做一件事情,你可以剥掉字符串并把它们放回去,但我真的很困惑如何用 $ 来做到这一点。任何帮助将不胜感激!!!

【问题讨论】:

  • print(f"${profit}")?
  • 由于你是python新手,我建议你先做一些研究,然后自己尝试一下。参考文档,寻找类似的问题,或者直接问你的教授。在这里发布问题会给你一个直接的答案,你会错过一个学习机会。

标签: python string integer


【解决方案1】:

也许你可以使用字符串格式:

print(f"${profit}")

【讨论】:

    【解决方案2】:

    你可以使用

    revenue = 98456
    costs = 45000
    profit = revenue - costs
    print("$ " +str(profit))
    

    会给

    $ 53456
    

    【讨论】:

      【解决方案3】:

      查看语言环境模块。

      这会进行货币(和日期)格式设置。

      >>> import locale
      >>> locale.setlocale( locale.LC_ALL, '' )
      'English_United States.1252'
      >>> locale.currency( 188518982.18 )
      '$188518982.18'
      >>> locale.currency( 188518982.18, grouping=True )
      '$188,518,982.18'
      

      一些例子:https://www.programcreek.com/python/example/6701/locale.currency

      【讨论】:

        猜你喜欢
        • 2018-06-22
        • 2018-02-08
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-13
        • 1970-01-01
        相关资源
        最近更新 更多