0 votes

Hello. I want to display variable in a label with 2 numbers after comma. It displays two numbers after a comma if numer is for example 2,45 but if it is 5 then it only shows "5" Instead of "5.00". Could someone help me please?

Godot version 3.2.3
in Engine by (21 points)

1 Answer

+1 vote
Best answer

Comma? , or FullStop? .

FullStop
$Label.text = str(my_var).pad_decimals(2)

Comma

func comma_separated_string(num : int):
    var string = str(num)
    for itr in range(0, len(str(num)), 3):
        if itr !=0:
            string = string.insert(len(str(num))-itr, ",")
    return string

$Label.text = comma_separated_string(my_var)
changing the '3' to where you want the commas placed

by (4,644 points)
selected by

And if you only want one "comma"
var string = str(my_var).pad_decimals(2)
$Label.text = string.replace(".", ",")

Thank you for help!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.