Python 将浮点数转换为美元和美分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21208376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Float to Dollars and Cents
提问by Evorlor
First of all, I have tried this post (among others): Currency formatting in Python. It has no affect on my variable. My best guess is that it is because I am using Python 3 and that was code for Python 2. (Unless I overlooked something, because I am new to Python).
首先,我尝试了这篇文章(以及其他文章):Python 中的货币格式。它对我的变量没有影响。我最好的猜测是因为我使用的是 Python 3 而那是 Python 2 的代码。(除非我忽略了一些东西,因为我是 Python 新手)。
I want to convert a float, such as 1234.5, to a String, such as "$1,234.50". How would I go about doing this?
我想将浮点数(例如 1234.5)转换为字符串,例如“$1,234.50”。我该怎么做呢?
And just in case, here is my code which compiled, but did not affect my variable:
以防万一,这是我编译的代码,但没有影响我的变量:
money = float(1234.5)
locale.setlocale(locale.LC_ALL, '')
locale.currency(money, grouping=True)
Also unsuccessful:
也失败了:
money = float(1234.5)
print(money) #output is 1234.5
'${:,.2f}'.format(money)
print(money) #output is 1234.5
采纳答案by π?δα? ?κ??
In Python 3.x and 2.7, you can simply do this:
在 Python 3.x 和 2.7 中,您可以简单地执行以下操作:
>>> '${:,.2f}'.format(1234.5)
',234.50'
The :,adds a comma as a thousands separator, and the .2flimits the string to two decimal places (or adds enough zeroes to get to 2 decimal places, as the case may be) at the end.
在:,增加了一个逗号作为千位分隔符,和.2f字符串限制到小数点后两位(或添加足够的零去小数点后2位,视情况而定)在最后。
回答by Alec
Building on @JustinBarber's example and noting @eric.frederich's comment, if you want to format negative values like -$1,000.00rather than $-1,000.00and don't want to use locale:
以@JustinBarber 的例子为基础,并注意@eric.frederich 的评论,如果你想格式化负值,比如-$1,000.00而不是$-1,000.00并且不想使用locale:
def as_currency(amount):
if amount >= 0:
return '${:,.2f}'.format(amount)
else:
return '-${:,.2f}'.format(-amount)
回答by Lewis
In python 3, you can use:
在python 3中,您可以使用:
import locale
locale.setlocale( locale.LC_ALL, 'English_United States.1252' )
locale.currency( 1234.50, grouping = True )
Output
输出
',234.50'
回答by Ariel Robaldo
you said that:
你之前这么说:
`mony = float(1234.5)
print(money) #output is 1234.5
'${:,.2f}'.format(money)
print(money)
did not work.... Have you coded exactly that way? This should work (see the little difference):
没有用......你是否完全那样编码?这应该有效(请参阅细微差别):
money = float(1234.5) #next you used format without printing, nor affecting value of "money"
amountAsFormattedString = '${:,.2f}'.format(money)
print( amountAsFormattedString )
回答by Rich Bayless
Personally, I like this much better (which, granted, is just a different way of writing the currently selected "best answer"):
就我个人而言,我更喜欢这个(当然,这只是编写当前选择的“最佳答案”的另一种方式):
money = float(1234.5)
print('$' + format(money, ',.2f'))
Or, if you REALLY don't like "adding" multiple strings to combine them, you could do this instead:
或者,如果你真的不喜欢“添加”多个字符串来组合它们,你可以这样做:
money = float(1234.5)
print('df_buy['BUY'] = df_buy['BUY'].astype('float')
df_buy['BUY'] = [' {:,.2f}'.format(i) for i in list(df_buy['BUY'])]
'.format(format(money, ',.2f')))
I just think both of these styles are a bit easier to read. :-)
我只是觉得这两种风格都更容易阅读。:-)
(of course, you can still incorporate an If-Else to handle negative values as Eric suggests too)
(当然,你仍然可以像 Eric 建议的那样合并一个 If-Else 来处理负值)

