pollen-not-included/utility/utility_strings.gd
Dan 2a9e78b52e Refactor: Added explicit typing and void return types
- Introduced explicit typing to variables and functions across multiple scripts for better code clarity.
- Specified 'void' as the return type for functions that do not return a value.
- Removed redundant code in some scripts.
2024-05-15 10:42:16 +01:00

37 lines
No EOL
1.1 KiB
GDScript

class_name StringUtilities extends Node
func seconds_to_hms(seconds: float) -> String:
var ms : float = fmod(seconds, 1) * 100
var s : float = fmod(seconds, 60)
var m : float = fmod(seconds, 3600) / 60
var formatted : String = "%02d:%02d:%02d" % [m, s, ms]
return formatted
####
## Formats the given number with commas every 3 digits.
####
func format_number(number: int) -> String:
# Handle negative numbers by adding the "minus" sign in advance, as we discard it
# when looping over the number.
var formatted_number : String = "-" if sign(number) == -1 else ""
var index : int = 0
var number_string : String = str(abs(number))
for digit : String in number_string:
formatted_number += digit
var counter : int = number_string.length() - index
# Don't add a comma at the end of the number, but add a comma every 3 digits
# (taking into account the number's length).
if counter >= 2 and counter % 3 == 1:
formatted_number += ","
index += 1
return formatted_number
func float_to_percentage(value: float) -> String:
return "%d%%" % int(value * 100)