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

I'd like to round the number 12.13 to 12 and 12.45 to 13.
None of the following functions return the format I'm looking for. I guess I could use if then function to get around it but, I'm sure there's an easy way to solve this.

TIA

import math

a = 12.13
b = 12.45

print(round(a), round(b))  # incorrect
print(math.ceil(a), math.ceil(b))  # incorrect
print(math.floor(a), math.floor(b))  # incorrect
print(math.trunc(a), math.trunc(b))  # incorrect
Reply
#2
What is the logic of your rounding? 12.45 should round to 12.
Reply
#3
(Dec-15-2021, 02:22 PM)deanhystad Wrote: What is the logic of your rounding? 12.45 should round to 12.
Because 12.45 should be 12.5 and therefore, 13.
12.13 should be 12.1 and therefore, 12.

More info: here
Reply
#4
ebolisa Wrote:Because 12.45 should be 12.5 and therefore, 13.
I have never seen this strange way of rounding numbers. Here you are rounding to the nearest dime, then to the nearest dollar. This is not the method described by the page that you linked above. In this page they say that one can round to the nearest dollar or to the nearest cent but they don't suggest to use one after the other.

Note that the results returned by round() are not what you could expect in limit cases
>>> round(11.5)
12
>>> round(12.5)
12
>>> round(13.5)
14
>>> round(14.5)
14
>>> round(15.5)
16
>>> round(16.5)
16
You could replace this by
from math import floor
def myround(x):
    return floor(x + 0.5)
>>> myround(11.5)
12
>>> myround(12.5)
13
>>> myround(13.5)
14
>>> myround(14.5)
15
>>> myround(15.5)
16
Reply
#5
That page appears to not agree with your logic:
Quote:Round down for a dollar amount that has 0 to 49 cents. For example, $89.27 rounds down to $89.

So 12.45 should round down to 12 if you're rounding to whole dollars according to that page because 45 is between 0 and 49.
Reply
#6
In Python 12.5 doesn't round to 13 either. Oops, I see Gribouillis included the odd case of 12.5 in his post.

Numerically your idea of cascaded rounding doesn't make any sense. Rounding should return the closest value and 12.45 is closer to 12 (0.45 difference) than it is 13 (0.55 difference). Your rounding has a positive bias of 0.05. I want my bank to use your rounding when calculating interest on my savings, but I don't want it used to calculate my taxes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 205 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Simple Question - ' defined as "a". ?' Ryan012 10 1,661 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 1,977 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,599 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,765 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Evaluating arithmetic expression with recursion. muddybucket 3 2,863 Dec-17-2021, 08:31 AM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,367 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,696 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  Magic Method Arithmetic Operators ClownPrinceOfCrime 3 2,332 Jan-10-2021, 03:24 PM
Last Post: ndc85430
  Simple question 1234 4 2,301 Dec-04-2020, 12:29 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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