I need help with a procedure to change the values of a label

I’m creating an application where I need to change the values ​​displayed on a label, in currency format, for example: R$ 0.00 to 5.00, 5,000.00 towards as the user clicks on the cards that show the values ​​to be displayed.

Here is the picture of the design AI

Screenshot_20250506-003441

These are the blocks I used to do the first formatting, but when pressing keys 2 and 3 the label is completely empty.

Capture

In this second format, pressing keys 2 and 3 leaves the label with the format R$ 0.002, R$ 0.003 or R$ 0.000, without deleting a character, so that it leaves it in the format R$ 0.02 or R$ 0.03.

Capture2

Just like the delete button deletes all the characters, it leaves the label without any digits, and does not return the format R$ 0.00.

If anyone can help me, I would appreciate it, as I am very uninformed about some procedures here at Kodular.

make block like this way:
blocks (3)

:white_check_mark: GOAL

Behavior:

  • Press 2 → Label shows R$ 0,02
  • Press 3 → Label shows R$ 0,23
  • Press 5 → Label shows R$ 2,35
  • Press delete → Removes last digit
  • Delete until empty → Label shows R$ 0,00

:brick: COMPONENTS

  • 10 Buttons (0–9) for digits
  • 1 Button for delete
  • 1 Label to display currency (e.g., LabelTotal)
  • (Optional: store input as a string)

:wrench: GLOBAL VARIABLES

initialize global RawInput to ""

This variable stores the numeric input as a string of digits (like "235" for R$ 2,35).


:1234: DIGIT BUTTON LOGIC

For each digit button (Button1, Button2, …, Button9, Button0):

when ButtonX.Click
do
   set global RawInput to join global RawInput (ButtonX.Text)
   call UpdateLabel

:back_arrow: DELETE BUTTON

when ButtonDelete.Click
do
   if length of global RawInput > 0 then
      set global RawInput to segment global RawInput from 1 to (length of global RawInput - 1)
   call UpdateLabel

:money_with_wings: UPDATE LABEL PROCEDURE

Create a procedure UpdateLabel:

procedure UpdateLabel
do
   if length of global RawInput = 0 then
      set LabelTotal.Text to "R$ 0,00"
   else
      // Ensure at least 3 characters for centavos
      set paddedInput to join "000" global RawInput
      set paddedInput to segment paddedInput from (length of paddedInput - 2) to length of paddedInput

      set reaisPart to segment paddedInput from 1 to (length of paddedInput - 2)
      set centavosPart to segment paddedInput from (length of paddedInput - 1) to length of paddedInput

      // Add thousands separator (optional, for large values)
      set reaisWithSeparator to call FormatWithThousandSeparator(reaisPart)

      set LabelTotal.Text to join "R$ " join reaisPart "," centavosPart

:abacus: THOUSAND SEPARATOR PROCEDURE (Optional)

If you want to add dots for thousands (5.000,00), build a helper block FormatWithThousandSeparator(text) to insert . accordingly.

Or start without it and keep the formatting simple.


:white_check_mark: EXAMPLE

Press sequence: 2 → 3 → 5

  • RawInput = "235"
  • reais = "2"
  • centavos = "35"
  • Display → R$ 2,35

After pressing delete:

  • RawInput = "23"
  • Display = R$ 0,23

if helpful mark as solution

2 Likes

Hello, good afternoon, I applied what you showed me, but it displays this error when I try to press a button.

Capturar3

and when I try to execute the procedure, I get this error

Capturar5

These are the blocks you showed me, but I also don’t know where to put the component that will do the formatting to make the separator worse by “.”, if you can help me, I appreciate it.

Capturar6

@Victor_Silva1
I am really sorry for my mistake.
here ur correct block.I tested it.it works fine.plz mark as solution if helpful

blocks (6)