3 examples of 'python format 2 decimal places' in Python

Every line of 'python format 2 decimal places' 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
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))
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

16def format_decimal(value):
17 return Decimal(value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))
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)

Related snippets