3 examples of 'how to print 2 decimal places in python' in Python

Every line of 'how to print 2 decimal places in python' 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
163def HumanReadableWithDecimalPlaces(number, decimal_places=1):
164 """Creates a human readable format for bytes with fixed decimal places.
165
166 Args:
167 number: The number of bytes.
168 decimal_places: The number of decimal places.
169 Returns:
170 String representing a readable format for number with decimal_places
171 decimal places.
172 """
173 number_format = MakeHumanReadable(number).split()
174 num = str(int(round(10**decimal_places * float(number_format[0]))))
175 if num == '0':
176 number_format[0] = ('0' +
177 (('.' +
178 ('0' * decimal_places)) if decimal_places else ''))
179 else:
180 num_length = len(num)
181 if decimal_places:
182 num = (num[:num_length - decimal_places] + '.' +
183 num[num_length - decimal_places:])
184 number_format[0] = num
185 return ' '.join(number_format)
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

114def 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))
9def print_scientific_double(value: float) -> str:
10 """
11 Prints a value in 16-character scientific double precision.
12
13 Scientific Notation: 5.0E+1
14 Double Precision Scientific Notation: 5.0D+1
15 """
16 if value < 0:
17 Format = "%16.9e"
18 else:
19 Format = "%16.10e"
20
21 svalue = Format % value
22 field = svalue.replace('e', 'D')
23
24 if field == '-0.0000000000D+00':
25 field = '0.0000000000D+00'
26 #assert len(field) == 16, ('value=%r field=%r is not 16 characters '
27 # 'long, its %s' % (value, field, len(field)))
28 return field

Related snippets