10 examples of 'python convert string to decimal' in Python

Every line of 'python convert string to decimal' 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
283def parse_decimal(string, locale=LC_NUMERIC):
284 """Parse localized decimal string into a decimal.
285
286 >>> parse_decimal('1,099.98', locale='en_US')
287 Decimal('1099.98')
288 >>> parse_decimal('1.099,98', locale='de')
289 Decimal('1099.98')
290
291 When the given string cannot be parsed, an exception is raised:
292
293 >>> parse_decimal('2,109,998', locale='de')
294 Traceback (most recent call last):
295 ...
296 NumberFormatError: '2,109,998' is not a valid decimal number
297
298 :param string: the string to parse
299 :param locale: the `Locale` object or locale identifier
300 :return: the parsed decimal number
301 :rtype: `Decimal`
302 :raise `NumberFormatError`: if the string can not be converted to a
303 decimal number
304 """
305 locale = Locale.parse(locale)
306 try:
307 return Decimal(string.replace(get_group_symbol(locale), '')
308 .replace(get_decimal_symbol(locale), '.'))
309 except InvalidOperation:
310 raise NumberFormatError('%r is not a valid decimal number' % string)
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

372def test__DECIMAL_to_python(self):
373 """Convert a MySQL DECIMAL to a Python decimal.Decimal type"""
374 data = b'3.14'
375 exp = Decimal('3.14')
376 res = self.cnv._DECIMAL_to_python(data)
377
378 self.assertEqual(exp, res)
379
380 self.assertEqual(self.cnv._DECIMAL_to_python,
381 self.cnv._NEWDECIMAL_to_python)
16def format_decimal(value):
17 return Decimal(value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))
78def pythonvalue(self, value):
79 return _Decimal(value)
10def convert_to_decimal(number, frac=False):
11 bad_type = not isinstance(number, (int, long, str, unicode, Decimal, Fraction))
12 if bad_type:
13 message = "{}, of type {} is not suitable as a Decimal"
14 logger.warning(message.format(number, type(number)))
15 return Fraction(number) if frac else Decimal(number)
143def split_decimal(value):
144 import decimal
145 decimal.getcontext().prec = 2
146 d = decimal.Decimal(value)
147 i = int(d)
148 return (i, d - i)
16def create_decimal(value, default=0):
17 """Create a decimal from the passed value. This is a decimal that
18 has 6 decimal places and is clamped between
19 -1 quadrillion < value < 1 quadrillion
20 """
21 from decimal import Decimal as _Decimal
22
23 if value is None:
24 return _Decimal(0, get_decimal_context())
25
26 try:
27 d = _Decimal("%.6f" % value, get_decimal_context())
28 except:
29 value = _Decimal(value, get_decimal_context())
30 d = _Decimal("%.6f" % value, get_decimal_context())
31
32 if d <= -1000000000000:
33 from Acquire.Accounting import AccountError
34 raise AccountError(
35 "You cannot create a balance with a value less than "
36 "-1 quadrillion! (%s)" % (value))
37
38 elif d >= 1000000000000000:
39 from Acquire.Accounting import AccountError
40 raise AccountError(
41 "You cannot create a balance with a value greater than "
42 "1 quadrillion! (%s)" % (value))
43
44 return d
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))
7def monzo_amount_to_dec(amount):
8 """
9 Monzo API returns monetary amount as an integer without the delimiter
10 before the subunit, so 12345 is in fact 123.45
11
12 :param amount: monetary amount
13 :type amount: int
14 :return converted monetary amount
15 :rtype Decimal
16 """
17 return Decimal(
18 str(amount)[:-2] + '.' + str(amount)[-2:]
19 )
589def _cast(self, value):
590 if value is not None and value != '':
591 return decimal.Decimal(value).quantize(self.precision)

Related snippets