10 examples of 'check float value' in Python

Every line of 'check float value' 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
637def is_float(value):
638 """
639 test if value is float
640 """
641 try:
642 float(value)
643 return True
644 except (ValueError, TypeError):
645 return False
38def _check_float(o):
39 if not isinstance(o, float):
40 raise TypeError("'{}' is not a 'float' object".format(o.__class__.__name__))
72def testFloat(val):
73 """
74 Test value for float.
75 Used to detect use of variables, strings and none types, which cannot be checked.
76 """
77 try:
78 return type(float(val)) == float
79 except Exception:
80 return False
4def check_positive(value):
5 try:
6 fvalue = float(value)
7 except ValueError:
8 return False
9 else:
10 if fvalue <= 0:
11 return False
12 return fvalue
48def range_check(value):
49 """ Checks that *value* can be converted to a value in the range 0.0 to 1.0.
50
51 If so, it returns the floating point value; otherwise, it raises a
52 TraitError.
53 """
54 value = float(value)
55 if 0.0 <= value <= 1.0:
56 return value
57 raise TraitError
87def check_negative(value):
88 e = argparse.ArgumentTypeError('{} is an invalid non-negative float value'.format(value))
89 try:
90 fvalue = float(value)
91 except ValueError:
92 raise e
93 if fvalue < 0:
94 raise e
95 return fvalue
8def isfloat(val):
9 """
10 check if the entry can become a float (float or string of float)
11
12 Parameters
13 ----------
14 val
15 an entry of any type
16
17 Returns
18 -------
19 bool
20 True if the input can become a float, False otherwise
21
22 """
23 try:
24 float(val)
25 return True
26 except ValueError:
27 return False
217def test_float():
218 Money = MoneyMaker()
219 money = Money(Decimal('sNaN'))
220 with pytest.raises(ValueError):
221 float(money)
222 money = Money(Decimal('NaN'))
223 assert math.isnan(float(money)) is True
224 money = Money(Decimal('-NaN'))
225 assert math.isnan(float(money)) is True
226 money = Money(Decimal('1.0'))
227 assert float(money) == 1.0
110def test_float_number():
111 """Tests to ensure the float type correctly allows floating point values"""
112 assert hug.types.float_number("1.1") == 1.1
113 assert hug.types.float_number("1") == float(1)
114 assert hug.types.float_number(1.1) == 1.1
115 with pytest.raises(ValueError):
116 hug.types.float_number("bacon")
152def is_float(var):
153 try:
154 float(var)
155 return True
156 except ValueError:
157 return False

Related snippets