Search code examples
pythonpython-3.xstring-parsingcurrency-formatting

Parse strings with different formats of decimal numbers to float


Is there in python a simple way to parse multiple different formats of possible decimal numbers into floats? Following formats could be used (usually amounts):

111 -> 111.00
111.00 -> 111.00
111,00 -> 111.00
11,111.00 -> 11111.00
11.111,00 -> 11111.00
11,111,111.00 -> 11111111.00
11.111.111,00 -> 11111111.00
111.111 -> 111111.00
111,111 -> 111111.00
111.111,00 -> 111111.00
111,111.00 -> 111111.00

At the moment I can only think of looking if there are different special characters ("," and ".") and then look which one is the decimal separator and so on. But that would be a lot of if/else I guess. I also tried locale.atof but the problem is, that I don't know which format the string is. I'm wondering if there is an easier and cleaner way.


Solution

  • This isn't the most efficient way to do it, but using regular-expression, we can split the variable into their different parts, and count the "last one" of them, to decide how to do the translation.

    import re
    
    values = """111
    111.00
    111,00
    11,111.00
    11.111,00
    11,111,111.00
    11.111.111,00
    111.111
    111,111
    111.111,00
    111,111.00"""
    
    values = [i for i in values.split("\n")]
    for value in values:
        value = re.split(r"\.|,", value.strip())
        if len(value[-1]) <=2 and len(value) > 1:
            newVal = float("".join(value[:-1]) + "." + value[-1])
        else:
            newVal =float("".join(value))
        print(newVal)
    

    Output:

    111.0
    111.0
    111.0
    11111.0
    11111.0
    11111111.0
    11111111.0
    111111.0
    111111.0
    111111.0
    111111.0
    

    EDIT: I didn't sanitize my inputs, the first version included some whitespace, throwing the translation into a loop.