Skip to content

Commit

Permalink
Fix number formatting (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
MewPurPur authored Aug 19, 2024
1 parent 3848659 commit 7af24ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit 7af24ad

Please sign in to comment.