9 examples of 'python float to string' in Python

Every line of 'python float to string' 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
48def format_float(x):
49 try:
50 return "%10.5f" % x
51 except:
52 return str(x)
4def 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)
24def 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))
5def fmtFloat(n):
6 return '{:6.3f}'.format(n)
383def floatstr(self, o):
384 # Check for specials. Note that this type of test is processor
385 # and/or platform-specific, so do tests which don't depend on
386 # the internals.
387 _inf = float('inf')
388 _neginf = -_inf
389
390 if o != o:
391 text = 'NaN'
392 elif o == _inf:
393 text = 'Infinity'
394 elif o == _neginf:
395 text = '-Infinity'
396 else:
397 return repr(o)
398
399 if self.ignore_nan:
400 text = 'null'
401 elif not self.allow_nan:
402 raise ValueError(
403 "Out of range float values are not JSON compliant: " +
404 repr(o))
405
406 return text
66def test_float(self):
67 self.assertEqual(sqlrepr(10.01), "10.01")
201def __str__(self):
202 return str(self.get_val())
28def get_float_from_string(s):
29 s = s.replace(',', '.') # due to different locales
30 pattern = r'\d{1,10}\.\d{1,10}'
31 match = re.search(pattern, s)
32 if match:
33 match = match.group(0)
34 result = float(match)
35 return result
36 return 0.0
70def float_to_hex(f):
71 """ Convert and x86 float to an ARM unsigned long int.
72
73 Args:
74 f (float): value to be converted
75 Raises:
76 Nothing
77 Returns:
78 str : representation of the hex value
79 """
80 return hex(struct.unpack('

Related snippets