10 examples of 'converting float to int in python' in Python

Every line of 'converting float to int in 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
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
141def int_or_float(x):
142 try:
143 return int(x)
144 except ValueError:
145 return float(x)
36def _int(val):
37 """
38 Parse string as an int, even if it has decimals.
39
40 >>> _int('1.0')
41 1
42 >>> _int('1.5')
43 1
44 """
45 return floor(float(val))
58def to_int(input):
59 try:
60 return int(input)
61 except ValueError:
62 raise Exception ('Input is not an integer')
197def _convert(self, value):
198
199 return int(value)
14def toint(num):
15 if num < 0:
16 return 0
17 elif num > 65535:
18 return 65535
19 else:
20 return int(num)
75def to_int(value):
76 return int(value)
84def to_float(num):
85 """Convert anything to float."""
86 return float(to_num(num))
62def str_to_int(x):
63 try:
64 return int(str_to_float(x))
65 except: # noqa E722
66 return x
74def int_or_float(string):
75 """Return int or float."""
76 try:
77 x = int(string)
78 return x
79 except ValueError:
80 x = float(string.replace(",", "."))
81 return x

Related snippets