Created
May 7, 2013 21:24
-
-
Save Newky/5536261 to your computer and use it in GitHub Desktop.
A fun little python class which allows you to write big numbers with commas for readability. Just for fun, not to be used for anything serious.
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
class NiceNumber(long): | |
def __new__(cls, *args): | |
x = 0 | |
if long(args[0]) >= 0: | |
sign = 1 | |
else: | |
sign = -1 | |
for i in range(0, len(args)): | |
reverse_i = (len(args) - 1) - i | |
x += (1000 ** i) * abs(long(args[reverse_i])) | |
return long.__new__(cls, x * sign) | |
if __name__ == "__main__": | |
x = NiceNumber(300,200,100,001) | |
assert x == 300200100001 | |
x = NiceNumber(200,100,001) | |
assert x == 200100001 | |
x = NiceNumber(100,001) | |
assert x == 100001 | |
x = NiceNumber(5,001) | |
assert x == 5001 | |
x = NiceNumber(5) | |
assert x == 5 | |
x = NiceNumber(-1) | |
assert x == -1 | |
x = NiceNumber(-1, 000) | |
assert x == -1000 | |
x = NiceNumber(-1, 001) | |
assert x == -1001 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment