0

I am very new to python, I would like to convert a string $45,000,000.00 into a float.

I am doing this by a very stupid way by slicing the string and then add them together...

test_data1 = "$45,300.000"
test_data1_1 = test_data1[1:3]
test_data1_2 = test_data1[4:7]
test_data1_3 = test_data1_1 + test_data1_2

c = float(test_data1_3)

since in this case, I was able to get 45300 as my desired int result, however, what if I have something like $ 23,456.78, then I am not able to convert it in this stupid way....so is there a more pythonic way to do this? Thanks so much!

Miss Nicole
  • 27
  • 1
  • 3
  • See my answer that uses regex to remove the unnecessary characters and then convert to float, it should work for everything! – Kevin Glasson Jul 28 '19 at 06:11
  • Possible duplicate of [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – ivan_pozdeev Jul 28 '19 at 06:16
  • Possible duplicate of [Parse currency into numbers in Python](https://stackoverflow.com/questions/37580151/parse-currency-into-numbers-in-python) – ivan_pozdeev Jul 28 '19 at 06:28

3 Answers3

5

If your number had no commas then you could just do what Mike Sperry has linked in the comments this. However, because that isn't the case, you can try this:

money = "$45,000,030.01"
moneyAsFloat = float(money.replace(",", "")[1:])
101arrowz
  • 1,825
  • 7
  • 15
0

This is the most general solution and it should work for all cases!!!

In [1]: import re

In [2]: x = re.sub("[^0-9|\.]","","$42,000,000.00")

In [3]: x
Out[3]: '42000000.00'

In [4]: float(x)
Out[4]: 42000000.0
Kevin Glasson
  • 408
  • 2
  • 13
0

Below is a general currency parser that doesn't rely on the babel library commonly used for this kind of problem.

import numpy as np
import re

def currency_parser(cur_str):
    # Remove any non-numerical characters
    # except for ',' '.' or '-' (e.g. EUR)
    cur_str = re.sub("[^-0-9.,]", '', cur_str)
    # Remove any 000s separators (either , or .)
    cur_str = re.sub("[.,]", '', cur_str[:-3]) + cur_str[-3:]

    if '.' in list(cur_str[-3:]):
        num = float(cur_str)
    elif ',' in list(cur_str[-3:]):
        num = float(cur_str.replace(',', '.'))
    else:
        num = float(cur_str)

    return np.round(num, 2)

Here is a pytest script that tests the function:

import numpy as np
import pytest
import re


def currency_parser(cur_str):
    # Remove any non-numerical characters
    # except for ',' '.' or '-' (e.g. EUR)
    cur_str = re.sub("[^-0-9.,]", '', cur_str)
    # Remove any 000s separators (either , or .)
    cur_str = re.sub("[.,]", '', cur_str[:-3]) + cur_str[-3:]

    if '.' in list(cur_str[-3:]):
        num = float(cur_str)
    elif ',' in list(cur_str[-3:]):
        num = float(cur_str.replace(',', '.'))
    else:
        num = float(cur_str)

    return np.round(num, 2)


@pytest.mark.parametrize('currency_str, expected', [
    (
            '.3', 0.30
    ),
    (
            '1', 1.00
    ),
    (
            '1.3', 1.30
    ),
    (
            '43,324', 43324.00
    ),
    (
            '3,424', 3424.00
    ),
    (
            '-0.00', 0.00
    ),
    (
            'EUR433,432.53', 433432.53
    ),
    (
            '25.675,26 EUR', 25675.26
    ),
    (
            '2.447,93 EUR', 2447.93
    ),
    (
            '-540,89EUR', -540.89
    ),
    (
            '67.6 EUR', 67.60
    ),
    (
            '30.998,63 CHF', 30998.63
    ),
    (
            '0,00 CHF', 0.00
    ),
    (
            '159.750,00 DKK', 159750.00
    ),
    (
            '£ 2.237,85', 2237.85
    ),
    (
            '£ 2,237.85', 2237.85
    ),
    (
            '-1.876,85 SEK', -1876.85
    ),
    (
            '59294325.3', 59294325.30
    ),
    (
            '8,53 NOK', 8.53
    ),
    (
            '0,09 NOK', 0.09
    ),
    (
            '-.9 CZK', -0.9
    ),
    (
            '35.255,40 PLN', 35255.40
    ),
    (
            '-PLN123.456,78', -123456.78
    ),
    (
            'US$123.456,79', 123456.79
    ),
    (
            '-PLN123.456,78', -123456.78
    ),
    (
            'PLN123.456,79', 123456.79
    ),
    (
            'IDR123.457', 123457
    ),
    (
            'JP¥123.457', 123457
    ),
    (
            '-JP\xc2\xa5123.457', -123457
    ),
    (
            'CN\xc2\xa5123.456,79', 123456.79
    ),
    (
            '-CN\xc2\xa5123.456,78', -123456.78
    ),
])
def test_currency_parse(currency_str, expected):
    assert currency_parser(currency_str) == expected
aj8907
  • 123
  • 1
  • 6