How to always display two numbers after comma?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Szaronko

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?

:bust_in_silhouette: Reply From: Wakatta

Comma? , or FullStop? .

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


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

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

Wakatta | 2021-02-22 00:38

Thank you for help!

Szaronko | 2021-02-22 07:25