Godot Version
4.6.1
Question
I’m looking for a clean way to show more than 1 decimal point in a spinbox.
I found this older post but I still had trouble using mouse clicks and setting up every action to trigger an update:
In the end I went with the code below which is based off the older post. Are there any better ways?
class_name SpinBoxPlus extends SpinBox
## A format field for numbers that allows for more control than [SpinBox].
##
## This can be used to, for example, always display values with a specific precision.
## [code]%.2f[/code] will display floats with two decimal places.
## A format string with a single specifier to display the box value.
## See [method String.format] for more information on format strings.
@export var format_string: String = "%.2f"
var last_value
# Convenience initializer that sets [member format_string] to [param fstring] if set.
func _init(fstring: String = "") -> void:
if not fstring.is_empty():
format_string = fstring
# Connect the signal handlers required to make this work seamlessly.
func _ready() -> void:
last_value = value
#Set this up initially
update()
# Check if updated
func _process(_delta: float) -> void:
if value != last_value:
last_value = value
update()
func update():
get_line_edit().text = (prefix + format_string + suffix) % value