Last active
June 19, 2021 00:40
-
-
Save luisgdev/6300f8b8dd25453b2b702e178c786cd4 to your computer and use it in GitHub Desktop.
Turns any bare float number into an easy readable amount.
This file contains hidden or 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
| # In: 12345678.90 (Float) | |
| # Out: 12,345,678.90 (String) | |
| number = float(12345678.90) | |
| n_digits = str(number).find('.') | |
| z_groups = int(n_digits/3) | |
| k_digits = n_digits % 3 | |
| group_separator = ',' | |
| print(f'Number: {number}') | |
| print(f'It has {n_digits} total int digits') | |
| print(f'It has {z_groups} groups of 3 int digits') | |
| print(f'It starts with {k_digits} digits') | |
| if k_digits == 0: | |
| k_digits = 3 | |
| z_groups -= 1 | |
| res = str(number)[0:k_digits] | |
| for i in range(z_groups): | |
| res += group_separator + str(number)[k_digits:k_digits+3] | |
| k_digits += 3 | |
| res += str(number)[n_digits:] | |
| print(f'Result: {res}') | |
| # Excludes too long numbers with e+X |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment