Python Forum
python decimal scale precision
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python decimal scale precision
#1
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
Reply
#2
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
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#3
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
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#4
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"
Reply
#5
(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
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#6
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Gmpy2 Newbie Working on Precision charlesrkiss 5 546 Jan-23-2024, 04:23 PM
Last Post: charlesrkiss
  Precision with Decimal Type charlesrkiss 9 726 Jan-18-2024, 06:30 PM
Last Post: charlesrkiss
  Python Struct Question, decimal 10, \n and \x0a. 3python 2 664 Aug-11-2023, 09:29 AM
Last Post: 3python
  How to properly scale text in postscript conversion to pdf? philipbergwerf 3 1,118 Nov-07-2022, 01:30 PM
Last Post: philipbergwerf
  Precision conversion garynewport 2 933 Oct-19-2022, 10:47 AM
Last Post: garynewport
  KeyError: 0 when trying to dedupe and match records at scale Catalytic 1 2,164 Apr-07-2022, 06:34 PM
Last Post: deanhystad
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,325 May-07-2021, 12:39 AM
Last Post: MDRI
  Matplotlib scale julienhofmann 0 1,800 Apr-04-2021, 08:50 AM
Last Post: julienhofmann
Photo Filtering data and precision during calculations Scientifix 0 1,782 Mar-30-2021, 01:00 PM
Last Post: Scientifix
  High-Precision Board Voltage Reading from Python into PD Liam484 1 2,079 Mar-29-2021, 02:57 PM
Last Post: Marbelous

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020