Skip to content

Instantly share code, notes, and snippets.

@pessini
Last active April 26, 2022 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pessini/b16498d08d3ab20e7150db918778846c to your computer and use it in GitHub Desktop.
Save pessini/b16498d08d3ab20e7150db918778846c to your computer and use it in GitHub Desktop.
Python uses banker’s rounding but not rounding away from zero. This function is a way to round a number away from zero (including negative numbers).
def round_up(x):
'''
Helper function to round away from zero
'''
from math import copysign
return int(x + copysign(0.5, x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment