Last active
November 28, 2017 08:52
-
-
Save soxofaan/cf78a872445d731821446749457cbf2c to your computer and use it in GitHub Desktop.
"Zequals" ruthless rounding: just keep one leading digit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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