10 examples of 'python decimal to float' in Python

Every line of 'python decimal to float' 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
84def to_float(num):
85 """Convert anything to float."""
86 return float(to_num(num))
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

10def convert_to_decimal(number, frac=False):
11 bad_type = not isinstance(number, (int, long, str, unicode, Decimal, Fraction))
12 if bad_type:
13 message = "{}, of type {} is not suitable as a Decimal"
14 logger.warning(message.format(number, type(number)))
15 return Fraction(number) if frac else Decimal(number)
96def __float__(self):
97 """Float representation."""
98 return float(Decimal(self))
16def format_decimal(value):
17 return Decimal(value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))
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
28def to_float(value):
29 '''
30 Returns the money value as a float.
31 '''
32 return value / float(INTERNAL_FACTOR)
60def test_decimal_int(self):
61 a, b = mathfilters.handle_float_decimal_combinations(Decimal('2.0'), 1, '+')
62 self.assertTrue(isinstance(a, Decimal), 'Type is {0}'.format(type(a)))
63 self.assertTrue(isinstance(b, int), 'Type is {0}'.format(type(b)))
66def test_float(self):
67 self.assertEqual(sqlrepr(10.01), "10.01")
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 )
238def test_as_decimal():
239 amount = EUR('1.23')
240 quantized_decimal = Decimal('1.23').quantize(Decimal('.01'))
241 assert amount.as_decimal() == quantized_decimal

Related snippets