10 examples of 'string to float python' in Python

Every line of 'string to float python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
28def get_float_from_string(s):
29 s = s.replace(',', '.') # due to different locales
30 pattern = r'\d{1,10}\.\d{1,10}'
31 match = re.search(pattern, s)
32 if match:
33 match = match.group(0)
34 result = float(match)
35 return result
36 return 0.0
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

23def textToFloat( value ):
24 if ',' in value and '.' in value:
25 commaIndex = value.rfind(',')
26 periodIndex = value.rfind('.')
27 if commaIndex > periodIndex:
28 newValue = value.replace( '.', '' ).replace( ',', '.' )
29 else:
30 newValue = value.replace( ',', '' )
31 elif ',' in value:
32 newValue = value.replace( ',', '.' )
33 else:
34 newValue = value
35 # Remove spaces
36 newValue = newValue.replace( ' ', '' )
37 # Remove possible coin symbol in the end
38 if not newValue[-1] in '0123456789':
39 newValue = newValue[:-1]
40 return float( newValue )
41def try_float(str):
42 try:
43 ret_val = float(str)
44 except ValueError:
45 ret_val = -1.
46 return ret_val
6def convert_string_to_float_int(string):
7
8 if isinstance(string, six.string_types):
9 try:
10 # evaluate the string to float, int or bool
11 value = literal_eval(string)
12 except Exception:
13 value = string
14 else:
15 value = string
16
17 # check if the the value is float or int
18 if isinstance(value, float):
19 if value.is_integer():
20 return int(value)
21 return value
22 return value
38def strToFloat(val):
39 try:
40 return float(val)
41 except ValueError:
42 return None
103def convert_string_to_float(string):
104 str_split = string.split('/')
105 return int(str_split[0]) / int(str_split[1]) # unit: mm
176def convert_to_float(val):
177 # percentages: (number%) -> float(number * 0.01)
178 m = re.search(r'([-\.\d]+)\%',
179 val if isinstance(val, basestring) else str(val), re.U)
180 try:
181 if m:
182 return float(m.group(1)) / 100 if m else val
183 if m:
184 return int(m.group(1)) + int(m.group(2)) / 60
185 except ValueError:
186 return val
187 # salaries: $ABC,DEF,GHI -> float(ABCDEFGHI)
188 m = re.search(r'\$[\d,]+',
189 val if isinstance(val, basestring) else str(val), re.U)
190 try:
191 if m:
192 return float(re.sub(r'\$|,', '', val))
193 except Exception:
194 return val
195 # generally try to coerce to float, unless it's an int or bool
196 try:
197 if isinstance(val, (int, bool)):
198 return val
199 else:
200 return float(val)
201 except Exception:
202 return val
220def isfloat(stringval):
221 try:
222 float(stringval)
223 return True
224 except (ValueError, TypeError), e: return False
45def convert_to_float(value, input_encoding):
46 value = unicode(value).lower().strip()
47 if not value or value in NULL:
48 return None
49
50 return locale.atof(value)
71def convertToFloat(tokens): return float(tokens[0])

Related snippets