-1
x = round(6.5)
y = round(5.5)
print(x)
print(y)

The result of both x and y happens to be 6. I am aware of the round function rounding exact half numbers to closest even integer to get more accurate data in statistics and stuff like that. So is there any way you can round of exact half integers to the closest "greater" integer instead of closest "even" integer without many errors

  • 4
    Does this answer your question? [Python 3.x rounding behavior](https://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior) – Ashwin Sankar Jun 27 '21 at 01:53
  • This comment does not give a way to actually round of half numbers to greater integers, so no – AYUSHMAN PARIDA Jun 27 '21 at 01:59
  • @AYUSHMANPARIDA What do you mean? If you click on the link and scroll down, it looks like the answers there answer your question. Please correct me if I'm wrong. – Ayush Garg Jun 27 '21 at 02:03

1 Answers1

1

I think you're looking for half round up which follows below:

import math
def round_half_up(n, decimals=0):
    multiplier = 10 ** decimals
    return math.floor(n*multiplier + 0.5) / multiplier

You should try to understand how the builtin round function works through the link provided, since it is meant to behave that way. It has its fair share of advantages and disadvantages