Python Forum
numbers with decimals
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numbers with decimals
#1
Hi,
I'm embarrassed, a member already answered this question, some time ago,
I can't find the reply any more. Blush .
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.
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#2
from decimal import Decimal


result = Decimal("1.337") * Decimal(1000)
Docs: https://docs.python.org/3/library/decimal.html
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Nov-28-2023, 11:47 AM)DeaD_EyE Wrote: from decimal import Decimal
Yep, thanks !
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#4
(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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
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))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(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.
« We can solve any problem by introducing an extra level of indirection »
Reply
#7
(Dec-01-2023, 08:54 AM)Gribouillis Wrote: It is a little too vague to me.
Ah, vague... Wink
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Forum Jump:

User Panel Messages

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