簡體   English   中英

如何在 Python 中將貨幣字符串轉換為浮點數?

[英]How do I convert a currency string to a floating point number in Python?

我有一些字符串表示具有特定貨幣格式的數字,例如:

money="$6,150,593.22"

我想將此字符串轉換為數字

6150593.22

實現這一目標的最佳方法是什么?

嘗試這個:

from re import sub
from decimal import Decimal

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

這有一些優點,因為它使用Decimal而不是float (更適合表示貨幣),並且它還通過不硬編碼特定的貨幣符號來避免任何語言環境問題。

如果您的語言環境設置正確,您可以使用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

對於沒有硬編碼貨幣頭寸或符號的解決方案:

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)

我發現babel非常有用

它使在本地化再現中解析數字變得容易:

>>> 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') /后綴,而無需對其進行硬編碼。

Hth, dtk

擴展以在括號中包含負數:

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() 都可用於將開頭 ( 轉換為 -,translate 稍快一些。float() 最快 10-15 倍,但缺乏精度,可能會出現語言環境問題。十進制( ) 具有精度並且比 locale.atof() 快 50%,但也存在語言環境問題。locale.atof() 是最慢的,但也是最通用的。

編輯:新的str.translate API(映射到None的字符從str.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)

幾年前我做了這個功能來解決同樣的問題。

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

此功能已將土耳其價格格式轉換為十進制數。

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

我發現的最簡單的方法,沒有硬編碼弄亂貨幣檢測,也使用了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

我將提供我的解決方案,希望它能幫助那些不僅面臨問題,而且還面臨問題的人. .

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        

擴展@Andrew Clark 的答案

對於不同於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,而不是浮點數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM