Every line of 'python f strings format 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.
48 def format_float(x): 49 try: 50 return "%10.5f" % x 51 except: 52 return str(x)
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))
5 def fmtFloat(n): 6 return '{:6.3f}'.format(n)
19 def latex_float(f): 20 float_str = "{0:.3g}".format(f) 21 if "e" in float_str: 22 base, exponent = float_str.split("e") 23 return r"{0} \times 10^{{{1}}}".format(base, int(exponent)) 24 else: 25 return float_str
54 @jit 55 def float_format(x): 56 """ 57 Format an integer: 58 59 - upcast to a double 60 - use snprintf 61 - resize buffer according to # of bytes written 62 """ 63 return formatting.format_static("%f", upcast(x), 20)
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)