【问题标题】:Converting Float to Dollars and Cents将浮点数转换为美元和美分
【发布时间】:2014-02-08 02:38:45
【问题描述】:

首先,我尝试过这篇文章(以及其他):Currency formatting in Python。它对我的变量没有影响。我最好的猜测是因为我使用的是 Python 3,而那是 Python 2 的代码。(除非我忽略了某些东西,因为我是 Python 新手)。

我想将浮点数(例如 1234.5)转换为字符串,例如“$1,234.50”。我该怎么做呢?

为了以防万一,这是我编译的代码,但不影响我的变量:

money = float(1234.5)
locale.setlocale(locale.LC_ALL, '')
locale.currency(money, grouping=True)

同样失败:

money = float(1234.5)
print(money) #output is 1234.5
'${:,.2f}'.format(money)
print(money) #output is 1234.5

【问题讨论】:

  • 后一个选项适用于 Python 2.7 和 3.3。
  • 如您的回答中所述,似乎不起作用
  • 您的代码还有其他问题。你能发布更多的上下文吗?
  • 我发布了更新版本。有什么想法吗?
  • 啊,你需要给'${:,.2f}'.format(money)赋值。例如尝试money = '${:,.2f}'.format(money),然后打印出money。

标签: python python-3.x floating-point string-formatting currency


【解决方案1】:
df_buy['BUY'] = df_buy['BUY'].astype('float')
df_buy['BUY'] = ['€ {:,.2f}'.format(i) for i in list(df_buy['BUY'])]

【讨论】:

    【解决方案2】:

    就个人而言,我更喜欢这个(当然,这只是编写当前选择的“最佳答案”的另一种方式):

    money = float(1234.5)
    print('$' + format(money, ',.2f'))
    

    或者,如果你真的不喜欢“添加”多个字符串来组合它们,你可以这样做:

    money = float(1234.5)
    print('${0}'.format(format(money, ',.2f')))
    

    我只是觉得这两种风格都更容易阅读。 :-)

    (当然,您仍然可以按照 Eric 的建议合并一个 If-Else 来处理负值)

    【讨论】:

      【解决方案3】:

      你说过:

      `mony = float(1234.5)
      print(money)      #output is 1234.5
      '${:,.2f}'.format(money)
      print(money)
      

      没有用.... 你是这样编码的吗? 这应该可以工作(见细微差别):

      money = float(1234.5)      #next you used format without printing, nor affecting value of "money"
      amountAsFormattedString = '${:,.2f}'.format(money)
      print( amountAsFormattedString )
      

      【讨论】:

        【解决方案4】:

        在 python 3 中,您可以使用:

        import locale
        locale.setlocale( locale.LC_ALL, 'English_United States.1252' )
        locale.currency( 1234.50, grouping = True )
        

        输出

        '$1,234.50'
        

        【讨论】:

        【解决方案5】:

        以@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)
        

        【讨论】:

        • 很好...这让我想到在没有定义的情况下在紧要关头这样做...布尔切片...“{}${:,.2f}".format(["" ,"-"][amount
        【解决方案6】:

        在 Python 3.x 和 2.7 中,您可以简单地这样做:

        >>> '${:,.2f}'.format(1234.5)
        '$1,234.50'
        

        :, 在末尾添加逗号作为千位分隔符,.2f 将字符串限制为两位小数(或添加足够的零以达到两位小数,视情况而定)。

        【讨论】:

        • '${:,.2f}'.format(money) 在 money = float(1234.5) 之后没有影响。我是不是搞错了?
        • @Evorlor 是的,这在 Python 3.3 和 2.7 中都适用于我。您是否将钱分配给需要打印的变量?
        • 是的,我通过前后印钱确认了这一点
        • 这不适用于负值。 '${:,.2f}'.format(-2) 返回 '$-2.00'。 locale.currency(-2, grouping=True) 返回“-$2.00”。只需导入语言环境并调用 locale.setlocale(locale.LC_ALL, '')
        • locale 有效,但这也适用于 python3:` (money
        猜你喜欢
        • 2021-03-31
        • 2020-03-21
        • 2015-08-23
        • 1970-01-01
        • 1970-01-01
        • 2021-12-25
        • 1970-01-01
        • 1970-01-01
        • 2021-12-29
        相关资源
        最近更新 更多