Every line of 'python convert float to decimal' 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.
10 def 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)
372 def test__DECIMAL_to_python(self): 373 """Convert a MySQL DECIMAL to a Python decimal.Decimal type""" 374 data = b'3.14' 375 exp = Decimal('3.14') 376 res = self.cnv._DECIMAL_to_python(data) 377 378 self.assertEqual(exp, res) 379 380 self.assertEqual(self.cnv._DECIMAL_to_python, 381 self.cnv._NEWDECIMAL_to_python)
60 def 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)))
66 def test_float(self): 67 self.assertEqual(sqlrepr(10.01), "10.01")
176 def 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
16 def format_decimal(value): 17 return Decimal(value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))
84 def to_float(num): 85 """Convert anything to float.""" 86 return float(to_num(num))
16 def create_decimal(value, default=0): 17 """Create a decimal from the passed value. This is a decimal that 18 has 6 decimal places and is clamped between 19 -1 quadrillion < value < 1 quadrillion 20 """ 21 from decimal import Decimal as _Decimal 22 23 if value is None: 24 return _Decimal(0, get_decimal_context()) 25 26 try: 27 d = _Decimal("%.6f" % value, get_decimal_context()) 28 except: 29 value = _Decimal(value, get_decimal_context()) 30 d = _Decimal("%.6f" % value, get_decimal_context()) 31 32 if d <= -1000000000000: 33 from Acquire.Accounting import AccountError 34 raise AccountError( 35 "You cannot create a balance with a value less than " 36 "-1 quadrillion! (%s)" % (value)) 37 38 elif d >= 1000000000000000: 39 from Acquire.Accounting import AccountError 40 raise AccountError( 41 "You cannot create a balance with a value greater than " 42 "1 quadrillion! (%s)" % (value)) 43 44 return d
23 def BN_convert_float(module): 24 ''' 25 Designed to work with network_to_half. 26 BatchNorm layers need parameters in single precision. 27 Find all layers and convert them back to float. This can't 28 be done with built in .apply as that function will apply 29 fn to all modules, parameters, and buffers. Thus we wouldn't 30 be able to guard the float conversion based on the module type. 31 ''' 32 if isinstance(module, torch.nn.modules.batchnorm._BatchNorm): 33 module.float() 34 for child in module.children(): 35 BN_convert_float(child) 36 return module
238 def test_as_decimal(): 239 amount = EUR('1.23') 240 quantized_decimal = Decimal('1.23').quantize(Decimal('.01')) 241 assert amount.as_decimal() == quantized_decimal