Issue
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?
Solution
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.
Answered By - Andrew Hare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.