Every line of 'python format float 2 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.
16 def format_decimal(value): 17 return Decimal(value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))
24 def float_str(f, min_digits=2, max_digits=6): 25 """ 26 Returns a string representing a float, where the number of 27 significant digits is min_digits unless it takes more digits 28 to hit a non-zero digit (and the number is 0 < x < 1). 29 We stop looking for a non-zero digit after max_digits. 30 """ 31 if f >= 1 or f <= 0: 32 return str(round_float(f, min_digits)) 33 start_str = str(round_float(f, max_digits)) 34 digits = start_str.split(".")[1] 35 non_zero_indices = [] 36 for i, digit in enumerate(digits): 37 if digit != "0": 38 non_zero_indices.append(i + 1) 39 # Only saw 0s. 40 if len(non_zero_indices) == 0: 41 num_digits = min_digits 42 else: 43 # Of the non-zero digits, pick the num_digit'th of those (including any zeros) 44 min_non_zero_indices = range(non_zero_indices[0], non_zero_indices[-1] + 1)[:min_digits] 45 num_digits = min_non_zero_indices[-1] 46 return str(round_float(f, num_digits))
48 def format_float(x): 49 try: 50 return "%10.5f" % x 51 except: 52 return str(x)
114 def decimal2text(value, places=2, 115 int_units=(('', '', ''), 'm'), 116 exp_units=(('', '', ''), 'm')): 117 value = decimal.Decimal(value) 118 q = decimal.Decimal(10) ** -places 119 120 integral, exp = str(value.quantize(q)).split('.') 121 return u'{} {}'.format( 122 num2text(int(integral), int_units), 123 num2text(int(exp), exp_units))
5 def fmtFloat(n): 6 return '{:6.3f}'.format(n)
55 def representacion(float, format=0, total=0, decimales=4, exp=False, tol=5, 56 signo=False, thousand=False): 57 """Function for string representation of float values 58 float: number to transform 59 format: mode 60 0 - fixed point 61 1 - Significant figures 62 2 - Engineering format 63 total: total number of digits 64 decimales: decimal number 65 exp: boolean to use engineering repr for big or small number 66 tol: exponent limit tu use engineering repr over float normal repr 67 signo: show sign 68 thousand: use thousan separator point 69 """ 70 if type(float) is str: 71 return float 72 73 if signo: 74 start = "{:+" 75 else: 76 start = "{: " 77 78 if thousand: 79 coma = ",." 80 else: 81 coma = "." 82 83 if exp and (-10**tol > float or (-10**-tol < float < 10**(-tol+1) and 84 float != 0) or float > 10**tol): 85 format = 2 86 if float == 0: 87 decimales = 1 88 89 if format == 1: 90 string = start+"{}{:d}g".format(coma, decimales)+"}" 91 elif format == 2: 92 string = start+"{:d}{}{:d}e".format(total, coma, decimales)+"}" 93 else: 94 string = start+"{:d}{}{:d}f".format(total, coma, decimales)+"}" 95 96 return string.format(float)
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")
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
4 def format_float_text(value): 5 v = value 6 if isinstance(v, u.Quantity): 7 v = value.value 8 elif isinstance(v, str): 9 v = float(value) 10 11 if 0.001 <= abs(v) <= 1000 or abs(v) == 0.0: 12 return "{0:.3f}".format(value) 13 else: 14 return "{0:.3e}".format(value)