Last active
June 15, 2024 08:04
-
-
Save CheeseCake87/db0806256eebb1bc8509ee04789823b4 to your computer and use it in GitHub Desktop.
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
def money_to_int(value): | |
""" | |
Converts a money value, commonly in float format 100.00 to an int value 10000 | |
""" | |
if not value: | |
return 0 | |
if isinstance(value, str): | |
if not value[0].isdigit(): | |
value = value[1:] # Remove potential currency symbol | |
if value.isdigit(): | |
return int(value) * 100 | |
else: | |
if "." in value: | |
rounder = round(float(value), 2) * 100 | |
return int(rounder) | |
if isinstance(value, int): | |
return value * 100 | |
if isinstance(value, float): | |
return int(value * 100) | |
return 0 | |
if __name__ == '__main__': | |
print("1 TEST", money_to_int("100.99") == 10099) | |
print("2 TEST", money_to_int("100") == 10000) | |
print("3 TEST", money_to_int(100) == 10000) | |
print("4 TEST", money_to_int(100.99) == 10099) | |
print("5 TEST", money_to_int("$100.99") == 10099) | |
print("6 TEST", money_to_int("$100") == 10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment