更新时间:2023-11-04 23:26:46
尝试一下:
from re import sub
from decimal import Decimal
money = '$6,150,593.22'
value = Decimal(sub(r'[^\d.]', '', money))
这具有一些优点,因为它使用 Decimal
而不是float
(这更适合表示货币),并且通过不对特定货币符号进行硬编码来避免任何语言环境问题.
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.