How to use 'python format two decimals' in Python

Every line of 'python format two decimals' 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
42def test_formatDecimals(self):
43 decimalNumber = '12.34'
44 decimalNumberWithMoreDecimals = '12.345678'
45 integerNumber = '12'
46 self.assertEqual(decimalNumber, formatDecimals(decimalNumberWithMoreDecimals))
47 self.assertEqual('12.00', formatDecimals(integerNumber))
48 self.assertEqual(decimalNumberWithMoreDecimals, formatDecimals(decimalNumberWithMoreDecimals, 6))
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))

Related snippets