Skip to content

Instantly share code, notes, and snippets.

@soxofaan
Last active November 28, 2017 08:52
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 soxofaan/cf78a872445d731821446749457cbf2c to your computer and use it in GitHub Desktop.
Save soxofaan/cf78a872445d731821446749457cbf2c to your computer and use it in GitHub Desktop.
"Zequals" ruthless rounding: just keep one leading digit.
import math
def zequals(x):
"""
"Zequals" ruthless rounding: just keep one leading digit.
examples:
123 -> 100
4567 -> 5000
-0.0345 -> -0.03
also see https://www.youtube.com/watch?v=aOJOfh2_4PE
@param x: int or float
@return: "zequalled" value
"""
if x == 0:
return 0
else:
b = math.pow(10, math.floor(math.log10(abs(x))))
return b * round(x / b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment