Hi,
I'm embarrassed, a member already answered this question, some time ago,
I can't find the reply any more.

.
I wrote a program for a friend to do some calculations, resulting numbers end with decimals.
I made an executable, and he used the program with success, although...
On close inspection, the decimals on his PC and on mine are slightly different. (same executable).
How come ?
A reply was given that I should use a special class of decimal numbers that does not have that problem.
Now I am about to write a new version, and I would like to implement that class, but I can't rememmber the name.
Anybody?
thx,
Paul
Edit : I seem to remember it's simply "the decimal module". See what we can find.
(Nov-28-2023, 11:47 AM)DeaD_EyE Wrote: [ -> ]from decimal import Decimal
"Decimal" does what it is supposed to do: produce machine independent decimals. No discussion.
But with all due respect: why do they write tutorials that are completely cryptic.
Why don't they start by explaining the simplest example first ? "I want to divide x / y with a precision of 2 decimals".
Turns out they use "Precision" for something else.
But, there is a solution: chatgpt.
I asked the question, and I got a perfectly sensible answer:
"rounded_number = number.quantize(Decimal('0.00'), rounding=ROUND_HALF_UP)"
In the maze of parameters they offer, it would have taken me all day to find that.
Paul
Example with localcontext how I would do it. But I use Decimal not often. Once per year I think to answer questions.
from decimal import Decimal
from decimal import ROUND_HALF_UP, ROUND_05UP
from decimal import localcontext
MY_CTX1 = localcontext(rounding=ROUND_HALF_UP)
MY_CTX2 = localcontext(rounding=ROUND_05UP)
with MY_CTX1:
print("Creating Decimals in localcontext with ROUND_HALF_UP")
val1 = Decimal("0.45")
val2 = Decimal("0.1")
print("Using regular python operators on Decimal")
result = val1 * val2
print()
print("Result:", result)
print("Rounded result in localcontext (2 digits):", round(result, 2))
print()
print("Rounded result outside of localcontext (2 digits):", round(result, 2))
with MY_CTX2:
print()
print("Value from old context, but using in a different context with ROUND_05UP")
print("Rounded result in localcontext (2 digits):", round(result, 2))
(Nov-29-2023, 07:35 AM)DPaul Wrote: [ -> ]Why don't they start by explaining the simplest example first ? "I want to divide x / y with a precision of 2 decimals".
A part of the problem is what do you mean exactly by
"I want to divide x / y with a precision of 2 decimals"? It is a little too vague to me.
(Dec-01-2023, 08:54 AM)Gribouillis Wrote: [ -> ]It is a little too vague to me.
Ah, vague...
You are free to ask what I mean by that question.
e.g. You might wonder about the rounding, up, down, none ...
Interaction is the way we develop better understanding of the problem.
In fact, the heart of this problem is not so much the decimals, but the
distribution of the remainder. e.g. How do you divide a cost of 1000 euro amongst three people,
given that you cannot receive less than 1000 euro ? Next month, same people, same amount. etc.
An age-old accounting problem.
Paul