5 examples of 'float to int python' in Python

Every line of 'float to int 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
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
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

78def bits2float( bits ):
79
80 # This is a bit convoluted, but this is much faster than ieee.pack
81 # stuff. In addition to normal casting through uint2singlefloat, we have
82 # additional casting because integer and float types that we can do
83 # arithmetic operations on are standard Python sizes (native machine
84 # size). Here's the typing going on below:
85 # Python Int (64-bit) -> r_uint32 -> r_singlefloat -> Python Float (64-bit)
86 flt = rffi.cast( lltype.Float, uint2singlefloat( r_uint32( bits ) ) )
87 return flt
141def int_or_float(x):
142 try:
143 return int(x)
144 except ValueError:
145 return float(x)
84def to_float(num):
85 """Convert anything to float."""
86 return float(to_num(num))
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.
394
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