Round function should be improved

the round function should be improved, the round function is supposed to round up numbers that are in a decimal format. I have found that the round function only looks at the first decimal point, for example when I enter this:
print(round(1.45), round(1.54))
as a result I get:
1 2

I have created some new code that works as the round function is supposed to:

def rounding(num):
    #this code was made by anon
    #this looks to see if the num is a decimal
    if num % 1 != 0:
          #sees if the first decimal digit is greater than or equal to 0.5
          if num % 1 >= 0.5:
              #retuns the number rounded up
              return int(num + 1)
      
        #sees if the first decimal digit is less than 0.5
        elif num % 1 < 0.4:
            #returns the number rounded down
            return int(num)
    
        #this is were things get tricky, this is for when the decimal is 0.4, or anything with 0.4 as the first decimal digit
        else:
            #create our list, which just stretches out the number
            stretch = []
            #this is the number we are going to be adding to the list
            stretch += str(num)
            #this is a latch to tell us if we have found the decimal point
            deci_found = False
      
            #this is were we look through the number
            for digit in stretch:
                #we want to know when we have found the decimal point
                if deci_found:
                    #this asks if the digit is greater than or equal to 5
                    if int(digit) >= 5:
                        #if it is, we add 1 to the number and return it
                        return int(num + 1)
          
                    #if it is not, we ask if it is less than 4
                    elif int(digit) < 4:
                        #if it is, we return the number
                        return int(num)
        
                  #we do not look for any 4s because those are the numbers that will be rounded up
                    
                #this finds the decimal point, and activates the latch if true
                if digit == ".":
                    deci_found = True
    
  
    #if the number is not a decimal, we just return the number
    else:
        return num

This is exactly what I would expect? Can you clarify what you think the output should be?

5 Likes

I ran the code (after fixing the indentation) and it output:

2 2

which is wrong.

1 Like

Looks correct to me. It isn’t clear to me what you think is incorrect
about this.

I presume you’ve read the documentation on round():

You’re thinking of math.ceil.

No. It’s not “round up”. Quoting docs:

Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

[…] values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2)

Python floats are actually binary float numbers, not decimal. There is decimal module to support decimal floating point math, you may want to use this:

>>> import decimal
>>> round(decimal.Decimal("1.45"), 0), round(decimal.Decimal("1.54"), 0)
(Decimal('1'), Decimal('2'))
>>> decimal.getcontext().rounding
'ROUND_HALF_EVEN'
>>> decimal.getcontext().rounding = decimal.ROUND_DOWN
>>> round(decimal.Decimal("1.45"), 0), round(decimal.Decimal("1.54"), 0)
(Decimal('1'), Decimal('1'))
>>> decimal.getcontext().rounding = decimal.ROUND_UP
>>> round(decimal.Decimal("1.45"), 0), round(decimal.Decimal("1.54"), 0)
(Decimal('2'), Decimal('2'))

I have found that the round function only looks at the first decimal point

No, that’s not true.

I guess so. But note, that round() function has optional ndigits argument. Rounding mode can’t be specified, but I’m not sure that it does make sense in this context.

4 Likes

thanks for the info