Last active
December 3, 2024 22:25
-
-
Save sebashtioon/f311d8cbdc9044ff13fd4b587ab45095 to your computer and use it in GitHub Desktop.
[Godot 4.3 stable] Format number with abbreviation function
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
| func format_number(number: int) -> String: | |
| if number >= 1_000: | |
| var i:float = snapped(float(number)/1_000, .01) | |
| return str(i).replace(",", ".") + "k" | |
| elif number >= 1_000_000: | |
| var i:float = snapped(float(number)/1_000_000, .01) | |
| return str(i).replace(",", ".") + "M" | |
| elif number >= 1_000_000_000: | |
| var i:float = snapped(float(number)/1_000_000_000, .01) | |
| return str(i).replace(",", ".") + "B" | |
| elif number >= 1_000_000_000_000: | |
| var i:float = snapped(float(number)/1_000_000_000_000, .01) | |
| return str(i).replace(",", ".") + "T" | |
| elif number >= 1_000_000_000_000_000: | |
| var i:float = snapped(float(number)/1_000_000_000_000_000, .01) | |
| return str(i).replace(",", ".") + "P" | |
| # NOTE: This function only formats numbers within the Quadrillion (P) range. | |
| # If you would like to add more prefixes, copy the elif statements and add them | |
| # below this comment (but before the else statement obviously) There is a template below: | |
| # (replace everything in the curly braces {} ) | |
| ## elif number >= {SOME_POWER_OF_TEN}: | |
| ## var i:float = snapped(float(number)/{SOME_POWER_OF_TEN}, .01) | |
| ## return str(i).replace(",", ".") + "{METRIC_PREFIX_LETTER}" | |
| else: | |
| # ran otherwise | |
| return str(number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment