5 examples of 'python convert float to int' in Python

Every line of 'python convert float to int' 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
6def convert_string_to_float_int(string):
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
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

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

84def to_float(num):
85 """Convert anything to float."""
86 return float(to_num(num))
141def int_or_float(x):
142 try:
143 return int(x)
144 except ValueError:
145 return float(x)
103def convert_string_to_float(string):
104 str_split = string.split('/')
105 return int(str_split[0]) / int(str_split[1]) # unit: mm
391def magic_int2float(magic_int):
392 """Convert a Python magic int into a 'canonic' floating-point number,
393 e.g. 2.7, 3.7. runtime error is raised if "version" is not found.
395 Note that there can be several magic_int's that map to a single floating-
396 point number. For example 3320 (3.5.a0), 3340 (3.5b1), and 3351 (3.5.2)
397 all map to 3.5.
398 """
399 return py_str2float(magicint2version[magic_int])

Related snippets