That one Decimal
it’s not like Decimal
firebird that’s guy Decimal
python
The decimal module is designed to support exact unrounded decimal arithmetic (fixed-point arithmetic) and floating-point round arithmetic.
To start using the decimal type you must first adjust your Operating context.
Contexts are environments for arithmetic operations. They govern accuracy, establish rules for rounding, determine which signs are treated as exceptions and limit the range of exponents.
The method decimal.getcontext()
returns the Context for the active thread.
>>> from decimal import *
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
This Context returned is the context in the absence. Their values are prec= 28
, rounding= ROUND_HALF_EVEN
, and rags to Overflow
, InvalidOperation
, and DivisionByZero
.
To adjust one of the context properties:
>>> getcontext().prec = 7
To set a new context use the method decimal.setcontext()
and pass as argument one of the predefined Contexts decimal.Defaultcontext
, decimal.BasicContext
and decimal.ExtendedContext
, or create your own context with the constructor decimal.Context()
.
Decimal instances can be constructed from integer numbers, strings, floating point or tuples:
>>> Decimal(3) / Decimal(4)
Decimal('0.75')
>>> data = list(map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split()))
>>> max(data)
Decimal('9.25')
>>> min(data)
Decimal('0.03')
>>> sorted(data)
[Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'),
Decimal('2.35'), Decimal('3.45'), Decimal('9.25')]
>>> sum(data)
Decimal('19.29')
>>> a,b,c = data[:3]
>>> str(a)
'1.34'
>>> float(a)
1.34
>>> round(a, 1)
Decimal('1.3')
>>> int(a)
1
>>> a * 5
Decimal('6.70')
>>> a * b
Decimal('2.5058')
>>> c % a
Decimal('0.77')
Transactions between decimals are closed to each other.
If you want the textual representation of a decimal convert it to string:
>>> str(Decimal(1) / Decimal(7))
'0.1428571'
>>> f'{Decimal(Decimal(2) * Decimal("0.34")):g}'
'0.68'
Or in your case:
cursor = conector.cursor()
cursor.execute("select e.descricao, e.preco_venda from estoque e where '7853849020424' = e.barras")
for row in cur.fetchall():
print(f'Item: {row[0]}, Valor: {row[1]:g}')
cursor.close()
conector.close()