3

I have a number of strings that represent numbers which use commas or points to separate thousands and have different floating separators. For example:

"22 000,76", "22.000,76", "22,000.76", "1022000,76", "-1,022,000.76", "1022000", "22 000,76$", "$22 000,76"

How can I convert these to float point numbers in Python?

In PHP I use function like this: http://docs.php.net/manual/sr/function.floatval.php#84793

5
  • Can you show us the output you need. I am getting a little confused with your example. Commented Feb 10, 2012 at 12:14
  • Example output: 22000.76, 22000.76, 22000.76, 1022000.76, -1022000.76, 1022000, 22000.76, 22000.76 - floating numbers Commented Feb 10, 2012 at 12:22
  • @Bondarenko: what about "100,000"? Commented Feb 10, 2012 at 12:30
  • @BondarenkoMikhail: What also? I think this could mean either 100000 or 100.0 Commented Feb 10, 2012 at 12:37
  • Full example: "22 000,76", "22.000,76", "22,000.76", "22 000","22,000","22.000","22000.76","22000,76","1.022.000,76","1,022,000.76","1,000,000","1.000.000","1022000.76","1022000,76","1022000","0.76","0,76","0.00","0,00","1.00","1,00","-22 000,76","-22.000,76","-22,000.76","-22 000","-22,000","-22.000","-22000.76","-22000,76","-1.022.000,76","-1,022,000.76","-1,000,000","-1.000.000","-1022000.76","-1022000,76","-1022000","-0.76","-0,76","-0.00","-0,00","-1.00","-1,00" Commented Feb 10, 2012 at 12:40

2 Answers 2

5
import re
import locale

# Remove anything not a digit, comma or period
no_cruft = re.sub(r'[^\d,.-]', '', st)

# Split the result into parts consisting purely of digits
parts = re.split(r'[,.]', no_cruft)

# ...and sew them back together
if len(parts) == 1:
    # No delimeters found
    float_str = parts[0]
elif len(parts[-1]) != 2:
    # >= 1 delimeters found. If the length of last part is not equal to 2, assume it is not a decimal part
    float_str = ''.join(parts)
else:
    float_str = '%s%s%s' % (''.join(parts[0:-1]),
                            locale.localeconv()['decimal_point'],
                            parts[-1])

# Convert to float
my_float = float(float_str)
Sign up to request clarification or add additional context in comments.

6 Comments

Don't use my answer if "100,000", as suggested by @Niklas B, is supposed to be 100000.
It might be interesting to use locale.localeconv()['decimal_point'] to make sure that float doesn't fail when the decimal point isn't '.' in the current locale.
Assuming from @BondarenkoMikhail last comment (which was removed(?)) all decimal parts, if present, are supposed to consist of 2 digits. Edited my answer to reflect this fact.
I don't think using locale is appropriate because the input is obviously from a mixture of locales -- although it arguably it is as a guess or to provide a default interpretation...
The builtin float() seems not to use locale to parse a string so setting locale.localeconv()['decimal_point'] for the decimal separator is not correct. It is better to set it to a fixed "."
|
0

Supposing you have at most 2 decimal digits:

sign_trans = str.maketrans({'$': '', ' ':''})
dot_trans = str.maketrans({'.': '', ',': ''})

def convert(num, sign_trans=sign_trans, dot_trans=dot_trans):
    num = num.translate(sign_trans)
    num = num[:-3].translate(dot_trans) + num[-3:]
    return float(num.replace(',', '.'))

I test it on your example:

>>> for n in nums:
...     print(convert(n))
...
22000.76
22000.76
22000.76
1022000.76
-1022000.76
1022000.0
22000.76
22000.76

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.