Python Forum
python decimal scale precision - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python decimal scale precision (/thread-20604.html)



python decimal scale precision - massimo_m - Aug-21-2019

Good morning, i'm starting a project in python, for a documents-of-transport, invoice, etc.
In this project i need to use the DECIMAL type, but i need to set the right scale precision (ex: for the VAT i need to have 1 digit, for the quantity in d.o.t. i need to have 2 digits).
In Postgresql i have the NUMERIC data type, that permits to set the scale precision, but i haven't find something similar in python.
The scale precision is fundamental (ex. here in italy the invoices must have 2 digit precision: if i multiply a price for 1.22 -the normal VAT in italy-, i need to keep the 2 digit in the result).

i see there is a context-environment precision, but no variable-intrinsic-precision


RE: python decimal scale precision - newbieAuggie2019 - Aug-21-2019

Hi!

I think you could use something like this:

print("\nBuongiorno.")
totale = input("Per favore, inserisci l'importo totale da pagare.\n\n")
decimali = float(totale)
print("\nL'importo totale è di %.2f"% round(decimali,2), "€. Grazie per fare affari con noi.")
And here I give you some inputs and outputs from running this little program:

Buongiorno. 
Per favore, inserisci l'importo totale da pagare.

24

L'importo totale è di 24.00 €. Grazie per fare affari con noi.
Buongiorno. 
Per favore, inserisci l'importo totale da pagare.

24.3

L'importo totale è di 24.30 €. Grazie per fare affari con noi.
Buongiorno. 
Per favore, inserisci l'importo totale da pagare.

24.35

L'importo totale è di 24.35 €. Grazie per fare affari con noi.
Buongiorno. 
Per favore, inserisci l'importo totale da pagare.

24.375

L'importo totale è di 24.38 €. Grazie per fare affari con noi.
Please, keep in mind that as it is, this program gives you the total amount with a 'dot' (.) to separate the integer digits from the decimal digits, and not the Italian 'virgola' (,), which would require much more twisting to the code.

I hope this could help you,

newbieAuggie2019


RE: python decimal scale precision - newbieAuggie2019 - Aug-22-2019

Hi again!

Actually, if you prefer the result with an Italian 'virgula' (,), instead of a 'full stop' (.), separating the integer digits from the decimal digits, you could use something like the following:

print("\nBuongiorno.")
totale = input("Per favore, inserisci l'importo totale da pagare.\nSe inserisci un numero con i decimali, prego di usare un punto (.) invece di una virgula (,).\n\nLe darò in seguito il totale con una virgula.\n\n")
decimali = float(totale)
approssimazione = "%.2f"% round(decimali,2)
print("\nL'importo totale è di " + approssimazione[0:-3] + "," + approssimazione[-2:] + " €. Grazie per fare affari con noi.")
Keep in mind, that now the program asks you to enter the number (if it has decimal digits), with a 'full stop' (.), instead of an Italian 'virgula' (,), and then the program will give you the result with an Italian 'virgula' (,).

And here I give you some inputs and outputs from running this little program:

Buongiorno.
Per favore, inserisci l'importo totale da pagare.
Se inserisci un numero con i decimali, prego di usare un punto (.), invece di una virgula (,).

Le darò in seguito il totale con una virgula.

24

L'importo totale è di 24,00 €. Grazie per fare affari con noi.
Buongiorno.
Per favore, inserisci l'importo totale da pagare.
Se inserisci un numero con i decimali, prego di usare un punto (.), invece di una virgula (,).

Le darò in seguito il totale con una virgula.

24.3

L'importo totale è di 24,30 €. Grazie per fare affari con noi.
Buongiorno.
Per favore, inserisci l'importo totale da pagare.
Se inserisci un numero con i decimali, prego di usare un punto (.), invece di una virgula (,).

Le darò in seguito il totale con una virgula.

24.35

L'importo totale è di 24,35 €. Grazie per fare affari con noi.
Buongiorno.
Per favore, inserisci l'importo totale da pagare.
Se inserisci un numero con i decimali, prego di usare un punto (.), invece di una virgula (,).

Le darò in seguito il totale con una virgula.

24.375

L'importo totale è di 24,38 €. Grazie per fare affari con noi.
If you are used to enter numbers with a decimal part, with an Italian 'virgula' (,), you could keep modifying the code to get that, but I think it could be easier to modify the configuration of your computer to use these numbers in the format "xxxxx,xx" instead of the format "xxxxx.xx", usually configurated as default.


I hope this could help you,

newbieAuggie2019


RE: python decimal scale precision - massimo_m - Aug-22-2019

Thank you, but in these examples you convert the float in decimal, while i would to have a variable with a arbitrary precision.
my needs are similar to this:



dec_value= myvalue

dec_value.setPrecision(2)

do some stuff

print dec_value.getPrecision()
and it gives me "2"


RE: python decimal scale precision - newbieAuggie2019 - Aug-22-2019

(Aug-22-2019, 08:27 AM)massimo_m Wrote: Thank you, but in these examples you convert the float in decimal, while i would to have a variable with a arbitrary precision.
my needs are similar to this:



dec_value= myvalue

dec_value.setPrecision(2)

do some stuff

print dec_value.getPrecision()
and it gives me "2"

Hi!

I'm not sure what you mean by arbitrary precision. I thought, according to your original post, that you wanted the totals (for V.A.T. needs) to be shown with 2 digits after the float point (decimal point, or virgula). This is exactly what this little program does (and it rounds the numbers to 2 significant float (decimal) places, it doesn't matter if the number is shorter or longer than that):

So if you introduce 24, the program prints 24.00 €,
if you introduce 24.3, the program prints 24.30 €,
if you introduce 24.35, the program prints 24.35 €,
if you introduce 24.375, the program prints 24.38 €.

newbieAuggie2019


RE: python decimal scale precision - perfringo - Aug-22-2019

(Aug-22-2019, 08:27 AM)massimo_m Wrote: my needs are similar to this:



dec_value= myvalue

dec_value.setPrecision(2)

do some stuff

print dec_value.getPrecision()
and it gives me "2"

You can use built-in decimal module.

>>> import decimal
>>> decimal.getcontext().prec = 2               # default precision is 28
>>> decimal.Decimal(1.7) / decimal.Decimal(2)
Decimal('0.85') 
>>> decimal.Decimal(1.7159) / decimal.Decimal(2)
Decimal('0.86')
>>> decimal.getcontext()
Context(prec=2, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[Inexact, FloatOperation, Rounded], traps=[InvalidOperation, DivisionByZero, Overflow])
You can set also rounding context etc.