如何在 Python 中将货币字符串转换为浮点数?

发布于 2024-12-20 02:34:03 字数 161 浏览 3 评论 0原文

我有一些表示具有特定货币格式的数字的字符串,例如:

money="$6,150,593.22"

我想将此字符串转换为数字

6150593.22

实现此目的的最佳方法是什么?

I have some strings representing numbers with specific currency format, for example:

money="$6,150,593.22"

I want to convert this string into the number

6150593.22

What is the best way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

[旋木] 2024-12-27 02:34:03

试试这个:

from re import sub
from decimal import Decimal

money = '$6,150,593.22'
value = Decimal(sub(r'[^\d.]', '', money))

这有一些优点,因为它使用 Decimal 而不是 float (这更适合表示货币),并且它还通过不硬编码特定的货币符号来避免任何区域设置问题。

Try this:

from re import sub
from decimal import Decimal

money = '$6,150,593.22'
value = Decimal(sub(r'[^\d.]', '', money))

This has some advantages since it uses Decimal instead of float (which is better for representing currency) and it also avoids any locale issues by not hard-coding a specific currency symbol.

划一舟意中人 2024-12-27 02:34:03

如果您的语言环境设置正确,您可以使用 locale.atof ,但您仍然需要手动去掉“$”:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF8')
'en_US.UTF8'
>>> money = "$6,150,593.22"
>>> locale.atof(money.strip("$"))
6150593.2199999997

If your locale is set properly you can use locale.atof, but you will still need to strip off the '$' manually:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF8')
'en_US.UTF8'
>>> money = "$6,150,593.22"
>>> locale.atof(money.strip("$"))
6150593.2199999997
我早已燃尽 2024-12-27 02:34:03

我发现 babel对于解决问题非常有帮助

  • <一href="https://stackoverflow.com/questions/8421922/how-do-i-convert-a-currency-string-to-a-floating-point-number-in-python#comment32554847_8422055">本地化 <一href="https://stackoverflow.com/questions/8421922/how-do-i-convert-a-currency-string-to-a-floating-point-number-in-python/8422012#comment10405860_8422012">解析< /a>
  • 以及需要 更改locale 全局

它使得解析本地化版本中的数字变得容易:

>>> babel.numbers.parse_decimal('1,024.64', locale='en')                                                                                                                           
Decimal('1024.64')
>>> babel.numbers.parse_decimal('1.024,64', locale='de')
Decimal('1024.64')
>>>

您可以使用 babel.numbers.get_currency_symbol('USD')去除前/后缀而不对其进行硬编码。

哈,
dtk

I found the babel package very helpful to work around

It makes it easy to parse a number in a localized rendition:

>>> babel.numbers.parse_decimal('1,024.64', locale='en')                                                                                                                           
Decimal('1024.64')
>>> babel.numbers.parse_decimal('1.024,64', locale='de')
Decimal('1024.64')
>>>

You can use babel.numbers.get_currency_symbol('USD') to strip pre/suffixes without hardcoding them.

Hth,
dtk

披肩女神 2024-12-27 02:34:03

对于无需对货币位置或符号进行硬编码的解决方案:

raw_price = "17,30 €"
import locale
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF8')
conv = locale.localeconv()
raw_numbers = raw_price.strip(conv['currency_symbol'])
amount = locale.atof(raw_numbers)

For a solution without hardcoding the currency position or symbol:

raw_price = "17,30 €"
import locale
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF8')
conv = locale.localeconv()
raw_numbers = raw_price.strip(conv['currency_symbol'])
amount = locale.atof(raw_numbers)
堇年纸鸢 2024-12-27 02:34:03

扩展以在括号中包含负数:

In [1]: import locale, string

In [2]: from decimal import Decimal

In [3]: n = ['$1,234.56','-$1,234.56','($1,234.56)', '$ -1,234.56']

In [4]: tbl = string.maketrans('(','-')

In [5]: %timeit -n10000 [locale.atof( x.translate(tbl, '$)')) for x in n]
10000 loops, best of 3: 31.9 æs per loop

In [6]: %timeit -n10000 [Decimal( x.translate(tbl, '$,)')) for x in n]
10000 loops, best of 3: 21 æs per loop

In [7]: %timeit -n10000 [float( x.replace('(','-').translate(None, '$,)')) for x in n]
10000 loops, best of 3: 3.49 æs per loop

In [8]: %timeit -n10000 [float( x.translate(tbl, '$,)')) for x in n]
10000 loops, best of 3: 2.19 æs per loop

请注意,必须从 float()/Decimal() 中删除逗号。带翻译表的replace() 或translate() 可用于将开头( 转换为-,翻译稍快一些。float() 最快10-15 倍,但缺乏精度,可能会出现区域设置问题。Decimal( )具有精度,并且比 locale.atof() 快 50%,但也存在区域设置问题。 locale.atof() 是最慢的,但最通用。

编辑:新的 str.translate API(字符)映射到 Nonestr.translate 函数移至翻译表)

In [1]: import locale, string
        from decimal import Decimal

        locale.setlocale(locale.LC_ALL, '')

        n = ['$1,234.56','-$1,234.56','($1,234.56)', '$ -1,234.56']

In [2]: tbl = str.maketrans('(', '-', '$)')
        %timeit -n10000 [locale.atof( x.translate(tbl)) for x in n]
18 µs ± 296 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [3]: tbl2 = str.maketrans('(', '-', '$,)')
        %timeit -n10000 [Decimal( x.translate(tbl2)) for x in n]
3.77 µs ± 50.8 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [4]: %timeit -n10000 [float( x.translate(tbl2)) for x in n]
3.13 µs ± 66.3 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [5]: tbl3 = str.maketrans('', '', '$,)')
        %timeit -n10000 [float( x.replace('(','-').translate(tbl3)) for x in n]
3.51 µs ± 84.8 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Expanding to include negative numbers in parentheses:

In [1]: import locale, string

In [2]: from decimal import Decimal

In [3]: n = ['$1,234.56','-$1,234.56','($1,234.56)', '$ -1,234.56']

In [4]: tbl = string.maketrans('(','-')

In [5]: %timeit -n10000 [locale.atof( x.translate(tbl, '$)')) for x in n]
10000 loops, best of 3: 31.9 æs per loop

In [6]: %timeit -n10000 [Decimal( x.translate(tbl, '$,)')) for x in n]
10000 loops, best of 3: 21 æs per loop

In [7]: %timeit -n10000 [float( x.replace('(','-').translate(None, '$,)')) for x in n]
10000 loops, best of 3: 3.49 æs per loop

In [8]: %timeit -n10000 [float( x.translate(tbl, '$,)')) for x in n]
10000 loops, best of 3: 2.19 æs per loop

Note that commas must be stripped from float()/Decimal(). Either replace() or translate() w/ a translation table can be used to convert the opening ( to -, translate is slightly faster. float() is fastest by 10-15x, but lacks precision and could present locale issues. Decimal() has precision and is 50% faster than locale.atof(), but also has locale issues. locale.atof() is the slowest, but most general.

Edit: new str.translate API (characters mapped to None moved from str.translate function to the translation table)

In [1]: import locale, string
        from decimal import Decimal

        locale.setlocale(locale.LC_ALL, '')

        n = ['$1,234.56','-$1,234.56','($1,234.56)', '$ -1,234.56']

In [2]: tbl = str.maketrans('(', '-', '$)')
        %timeit -n10000 [locale.atof( x.translate(tbl)) for x in n]
18 µs ± 296 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [3]: tbl2 = str.maketrans('(', '-', '$,)')
        %timeit -n10000 [Decimal( x.translate(tbl2)) for x in n]
3.77 µs ± 50.8 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [4]: %timeit -n10000 [float( x.translate(tbl2)) for x in n]
3.13 µs ± 66.3 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [5]: tbl3 = str.maketrans('', '', '$,)')
        %timeit -n10000 [float( x.replace('(','-').translate(tbl3)) for x in n]
3.51 µs ± 84.8 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
叫嚣ゝ 2024-12-27 02:34:03

扩展 @Andrew Clark answer

对于不同于 en_US 的其他语言环境:

>>> import re
>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, 'pt_BR.UTF8') # this is for atof()
'pt_BR.UTF8'
>>> locale.setlocale(locale.LC_MONETARY, 'pt_BR.UTF8') # this is for currency()
'pt_BR.UTF8'
>>> curr = locale.currency(6150593.22, grouping = True)
>>> curr
'R$ 6.150.593,22'
>>> re.sub('[^(\d,.)]', '', curr)
'15,00'
>>> locale.atof(re.sub('[^(\d,.)]', '', curr))
6150593.22
>>> 6150593.22 == locale.atof(re.sub('[^(\d,.)]', '', locale.currency(6150593.22, grouping = True)))
True

强制提醒:适当的Python 的货币类型是 Decimal,而不是浮点数。

Expanding on @Andrew Clark answer

For other locales different than en_US:

>>> import re
>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, 'pt_BR.UTF8') # this is for atof()
'pt_BR.UTF8'
>>> locale.setlocale(locale.LC_MONETARY, 'pt_BR.UTF8') # this is for currency()
'pt_BR.UTF8'
>>> curr = locale.currency(6150593.22, grouping = True)
>>> curr
'R$ 6.150.593,22'
>>> re.sub('[^(\d,.)]', '', curr)
'15,00'
>>> locale.atof(re.sub('[^(\d,.)]', '', curr))
6150593.22
>>> 6150593.22 == locale.atof(re.sub('[^(\d,.)]', '', locale.currency(6150593.22, grouping = True)))
True

The obligatory reminder: The appropriate Python type for currency is Decimal, not floating points.

橘味果▽酱 2024-12-27 02:34:03

几年前我做了这个函数来解决同样的问题。

def money(number):
    number = number.strip('
)
    try:
        [num,dec]=number.rsplit('.')
        dec = int(dec)
        aside = str(dec)
        x = int('1'+'0'*len(aside))
        price = float(dec)/x
        num = num.replace(',','')
        num = int(num)
        price = num + price
    except:
        price = int(number)
    return price

I made this function a few years ago to solve the same problem.

def money(number):
    number = number.strip('
)
    try:
        [num,dec]=number.rsplit('.')
        dec = int(dec)
        aside = str(dec)
        x = int('1'+'0'*len(aside))
        price = float(dec)/x
        num = num.replace(',','')
        num = int(num)
        price = num + price
    except:
        price = int(number)
    return price
心的位置 2024-12-27 02:34:03

该函数已将土耳其价格格式转换为十进制数。

money = '1.234,75'
def make_decimal(string):
    result = 0
    if string:
        [num, dec] = string.rsplit(',')
        result += int(num.replace('.', ''))
        result += (int(dec) / 100)
    return result
print(make_decimal(money))
1234.75

this function has convert turkish price format to decimal number.

money = '1.234,75'
def make_decimal(string):
    result = 0
    if string:
        [num, dec] = string.rsplit(',')
        result += int(num.replace('.', ''))
        result += (int(dec) / 100)
    return result
print(make_decimal(money))
1234.75
零崎曲识 2024-12-27 02:34:03

我发现的最简单的方法,无需对货币检测进行硬编码,还可以使用 Decimal 类型,从而避免 float 类型的问题:

>>> from decimal import Decimal
>>> money="$6,150,593.22"
>>> amount = Decimal("".join(d for d in money if d.isdigit() or d == '.'))
>>> amount
Decimal('6150593.22')

credit: https://www.reddit.com/r/learnpython/comments/2248mp/how_to_format_currency_without_currency_sign/cgjd1o4?utm_source=share&utm_medium=web2x

Simplest way I found, without hard-coding on messing with currency detection, also uses the Decimal type which avoids issues with the float type:

>>> from decimal import Decimal
>>> money="$6,150,593.22"
>>> amount = Decimal("".join(d for d in money if d.isdigit() or d == '.'))
>>> amount
Decimal('6150593.22')

credit: https://www.reddit.com/r/learnpython/comments/2248mp/how_to_format_currency_without_currency_sign/cgjd1o4?utm_source=share&utm_medium=web2x
弄潮 2024-12-27 02:34:03

我将提供我的解决方案,希望它能帮助那些不仅面临 , 问题,而且还面临 . 问题的人。

def process_currency_adaptive(currency_string: str, decimal_sep_char: str) -> float:
    """
    Converts the currency string to common float format:
        Format: 
            ######.### 
        Example: 
            6150593.22
    """
    # Get rid of currency symbol
    currency_symbols = ["$", "€", "£", "₺"]
    
    # Replace any occurrence of currency symbol with empty string
    for symbol in currency_symbols:
        currency_string = currency_string.replace(symbol, "")
    
    
    if decimal_sep_char == ",":
        triple_sep_char = "."
    elif decimal_sep_char == ".":
        triple_sep_char = ","
    else:
        raise ValueError("Invalid decimal separator character: {}".format(decimal_sep_char))

    # Get rid of the triple separator
    currency_string = currency_string.replace(triple_sep_char, "")
    
    # There should be only one decimal_sep_char.
    if currency_string.count(decimal_sep_char) != 1:
        print("Error: Invalid currency format with value: {}".format(currency_string))
        raise ValueError
    
    return float(currency_string.replace(decimal_sep_char, "."))

# test process_currency
print(process_currency_adaptive("942,695", decimal_sep_char=","))  # 942.695
print(process_currency_adaptive("$6,150,593.22", decimal_sep_char="."))  # 6150593.22        

I'll provide my solution, hoping it would help someone who face problems with not just , but also ..

def process_currency_adaptive(currency_string: str, decimal_sep_char: str) -> float:
    """
    Converts the currency string to common float format:
        Format: 
            ######.### 
        Example: 
            6150593.22
    """
    # Get rid of currency symbol
    currency_symbols = ["
quot;, "€", "£", "₺"]
    
    # Replace any occurrence of currency symbol with empty string
    for symbol in currency_symbols:
        currency_string = currency_string.replace(symbol, "")
    
    
    if decimal_sep_char == ",":
        triple_sep_char = "."
    elif decimal_sep_char == ".":
        triple_sep_char = ","
    else:
        raise ValueError("Invalid decimal separator character: {}".format(decimal_sep_char))

    # Get rid of the triple separator
    currency_string = currency_string.replace(triple_sep_char, "")
    
    # There should be only one decimal_sep_char.
    if currency_string.count(decimal_sep_char) != 1:
        print("Error: Invalid currency format with value: {}".format(currency_string))
        raise ValueError
    
    return float(currency_string.replace(decimal_sep_char, "."))

# test process_currency
print(process_currency_adaptive("942,695", decimal_sep_char=","))  # 942.695
print(process_currency_adaptive("$6,150,593.22", decimal_sep_char="."))  # 6150593.22        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文