Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix number formatting #868

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/data_classes/AttributeNumeric.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var _number := NAN
func _sync() -> void:
if not _value.is_empty():
_number = text_to_num(_value)
_percentage = _value.strip_edges().ends_with("%")
_percentage = text_check_percentage(_value)

func set_num(new_number: float) -> void:
_number = new_number
Expand All @@ -20,6 +20,13 @@ func is_percentage() -> bool:
return _percentage


func format(text: String) -> String:
var num := text_to_num(text)
if text_check_percentage(text):
return num_to_text(num * 100.0) + "%"
else:
return num_to_text(num)

func num_to_text(number: float) -> String:
return NumberParser.num_to_text(number, formatter)

Expand All @@ -30,3 +37,6 @@ static func text_to_num(text: String) -> float:
if text.ends_with("%"):
return text.to_float() / 100
return text.to_float()

func text_check_percentage(text: String) -> bool:
return text.strip_edges().ends_with("%")
4 changes: 4 additions & 0 deletions src/data_classes/NumberParser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ static func num_to_text(number: float, formatter: Formatter) -> String:
if not is_finite(number):
return ""

if number == -0.0:
number = absf(number)

if formatter.number_use_exponent_if_shorter and absf(number) >= 1000.0:
var ending_zeros := 0
while is_zero_approx(fmod(number, 10 ** (ending_zeros + 1))):
Expand All @@ -19,4 +22,5 @@ static func num_to_text(number: float, formatter: Formatter) -> String:
output = output.right(-1)
elif output.begins_with("-0"):
output = output.erase(1)

return output