Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rounding question
#1
Hi,

Today python (math) offers many tools to modify the float 5.49
into something else. int(), floor(), ceil()...

But i have not found an equivalent for something we were taught many, many decades ago:
int(5.49 + 0.50) wil give 5, but int(5.50 + 0.50) (and upwards) will give 6.
Kind of random rounding when distributing costs. Accountants liked that.

Question: what python function will do that without the hanky-panky of adding a half before truncating?

thx,
Paul
Reply
#2
not sure what you ask
>>> int(5.49 + 0.50)
5
>>> int(5.50 + 0.50)
6
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Are you looking to round in a way other than what round() already supports?

>>> round(5.49)
5
>>> round(5.50)
6
Reply
#4
Thanks, but no, that is not what i am looking for.

Imagine you are doing many divisions, that produce numbers with decimals.(say distributing costs over 5 departments)
But you don't want decimals on your report (or in your accounting system for that matter).
int() will cut off the decimals, but if you are allocating costs, you will come up short at the end of the day.
(you lost all of the cents)
if you do int( x + 0.50) , this will produce a floor() or a ceil() depending on the value of the decimal.
"random" so to speak.
So i am wondering if python offers a function that will do this without adding the 0.50.
It could be that this age old trick is still necessary.

EDIT : if you think this is a strange question: why then did they invent "ceil()" ? Adding 1 instead of 0.50 does that trick also. :-)

Paul
Reply
#5
(Apr-11-2020, 08:41 AM)DPaul Wrote: But you don't want decimals on your report (or in your accounting system for that matter).
Actually I do want decimals in my accounting system, up to the last cent :-) And in some cases like interest/coupon accrual rounding is only at the end of the accrual period


int will always truncate downwards, when argument is float:

Quote:For floating point numbers, this truncates towards zero.

>>> int(5.99)
5
In your example with 5.50 + 0.50 it was the same - truncated toward 6
>>> d = 5.50 + 0.50
>>> d
6.0
>>> int(d)
6
(Apr-11-2020, 08:41 AM)DPaul Wrote: "random" so to speak.
Nothing random here

When working with money, better work with decimal.Decimal to avoid floating-point arithmetic surprises.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
I am not impressed by the rounding capabilities of Python either. If I would have to make a program where rounding is important, I would consider using the decimal module. The manual states:
Quote:computers must provide an arithmetic that works in the same way as the arithmetic that people learn at school.
Reply
#7
For your information.
There are countries with no decimals in their currency.
Before the euro, there were more.
But this has nothing to do with my question. :-)

Paul.
Reply
#8
(Apr-11-2020, 09:42 AM)DPaul Wrote: There are countries with no decimals in their currency.
I am fully aware of that. This is not the case here as I understand it...
And there are countries in eurozone that consider or already getting rid of 1 and 2 cent coins and round all prices to 0.05
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Why don't we stick to the question,
and stop speculating about what i want to do with it.
If you look the avatar i was given, i'm only one of the two. :-)

Paul
Reply
#10
(Apr-11-2020, 10:00 AM)DPaul Wrote: Why don't we stick to the question,
Fair point. Though it's not clear what you want... Did you see my reply here? The int() will always round downwards, nothing random about it functionality... If there is still open issue, please, post sample input and expected output. Not some example of "adding 0.50"...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need help rounding joseph202020 7 1,321 Feb-21-2023, 08:13 PM
Last Post: joseph202020
  from numpy array to csv - rounding SchroedingersLion 6 2,170 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  Random data generation sum to 1 by rounding juniorcoder 9 3,422 Oct-20-2021, 03:36 PM
Last Post: deanhystad
  Rounding issue kmll 1 1,417 Oct-08-2021, 10:35 AM
Last Post: Yoriz
  Not rounding to desired decimal places? pprod 2 2,560 Mar-05-2021, 11:11 AM
Last Post: pprod
  Decimal Rounding error project_science 4 2,760 Jan-06-2021, 03:14 PM
Last Post: project_science
  rounding and floats Than999 2 3,095 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  Rounding to the nearest eight wallgraffiti 2 2,076 Jul-15-2020, 06:05 PM
Last Post: wallgraffiti
  price + tax rounding mlieqo 11 6,454 Sep-21-2019, 04:53 PM
Last Post: mlieqo
  rounding floats to a number of bits Skaperen 2 2,310 Sep-13-2019, 04:37 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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