Search code examples
floating-pointjythonstring-parsing

Jython: parsing text to float containing commas


How can I parse a float scanned from a sheet as text, containing commas?

txt = "1,903.44"
value = float(txt) # This fails due to ',' in string

UPDATE: Sorry I wasn't clear. I'm using jython 2.5, which doesn't have the locale module.


Solution

  • txt = "1,903.44"
    value = float(txt.replace(',', ''))
    

    If you need localization, this won't really work but it does the trick if you know that commas are your separators.