How to add a number next to a number with a button?

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

Hi i’m working on a Cost Calculator and would like to know how I would get it to add on the numbers and such when you click the corresponding button, example you click 5 and it reads “5” click 2 and it adds 2 to it making it “52” than clicking . it’d add a . so it’d be “52.” than clicking 9 it’d ofc add 9 and make it “52.9” how would I do something like this?
Here is my current code.

extends Node2D

var subtotal

func _ready():
$CanvasLayer/SubTotalN.text = str(subtotal)

func _on_0Button_pressed():
subtotal + 0

:bust_in_silhouette: Reply From: magicalogic

What you need is a variable to hold the part of the number already entered then when a button is pressed, concatenate the part of the number with the entered one. Then set the variable holding your number to the result.

I mean like this:

Suppose you decide to name you variable number,

var number

When button one is pressed, adjust number like this:

number = number + "1" 

Do that for all your buttons.
To do calculations you will have to cast number to a float or int with
float(number) or int(number)