如何让 python 中的号码与 $ 一起显示?
How do I get my numbers in python to show up with $?
我在读高中,这是我第一次尝试 python。我希望我的等式在数字前用 $ 打印。这是我现在的代码:
revenue = 98456
costs = 45000
profit = revenue - costs
print(profit)
我的教授说你可以做一些事情,你可以把绳子拆下来再放回去,但我真的很困惑如何用 $.任何帮助将不胜感激!!!
也许你可以使用字符串格式:
print(f"${profit}")
您可以使用
revenue = 98456
costs = 45000
profit = revenue - costs
print("$ " +str(profit))
会给
$ 53456
查看语言环境模块。
这会进行货币(和日期)格式化。
>>> import locale
>>> locale.setlocale( locale.LC_ALL, '' )
'English_United States.1252'
>>> locale.currency( 188518982.18 )
'8518982.18'
>>> locale.currency( 188518982.18, grouping=True )
'8,518,982.18'
一些示例:https://www.programcreek.com/python/example/6701/locale.currency
我在读高中,这是我第一次尝试 python。我希望我的等式在数字前用 $ 打印。这是我现在的代码:
revenue = 98456
costs = 45000
profit = revenue - costs
print(profit)
我的教授说你可以做一些事情,你可以把绳子拆下来再放回去,但我真的很困惑如何用 $.任何帮助将不胜感激!!!
也许你可以使用字符串格式:
print(f"${profit}")
您可以使用
revenue = 98456
costs = 45000
profit = revenue - costs
print("$ " +str(profit))
会给
$ 53456
查看语言环境模块。
这会进行货币(和日期)格式化。
>>> import locale
>>> locale.setlocale( locale.LC_ALL, '' )
'English_United States.1252'
>>> locale.currency( 188518982.18 )
'8518982.18'
>>> locale.currency( 188518982.18, grouping=True )
'8,518,982.18'
一些示例:https://www.programcreek.com/python/example/6701/locale.currency