Edit Currency Delphi Firemonkey

Asked

Viewed 2,189 times

5

I need to format an Edit in the format 0,00 in Firemonkey, using preferably the event ChangeTracking. I tried to use the following procedure that did not resolve.

procedure FormatadorMoeda(pEdit: TEdit);
var
  loStr: string;
  loDouble: double;
begin
  loStr := pEdit.Text;

  if loStr = EmptyStr then
    loStr := '0,00';

  loStr := Trim(StringReplace(loStr, '.', '', [rfReplaceAll, rfIgnoreCase]));
  loStr := Trim(StringReplace(loStr, ',', '', [rfReplaceAll, rfIgnoreCase]));

  loDouble := StrToFloat(loStr);
  loDouble := (loDouble / 100);
  pEdit.Text := FormatFloat('###,##0.00', loDouble);
  pEdit.SelStart := Length(pEdit.Text);
end;

2 answers

4

Follow the method for formatting:

function TForm1.DisplayFormatter(AValue: double; ADisplayFormar: String): String;
begin
  Result := FormatFloat(ADisplayFormar, AValue);
end;

I wouldn’t advise using this guy on ChangeTracking, the cool thing is you shoot this method when the guy finishes filling the field.

Follow an example of use:

procedure TForm1.Button1Click(Sender: TObject);
var
  iAux: Double;
begin
  if (Edit1.Text = EmptyStr) then
    Edit1.Text := '0';

  if TryStrToFloat(Edit1.Text, iAux) then
    Edit1.Text := DisplayFormatter(StrToFloat(Edit1.Text), ('#0.00'));
end;

Note: Could implement a key lock on Edit, if it is "Numbersonly"

  • 1

    It needed the behavior of Edit pit similar to the monetary drive fields that are used on the bank sites, instant formatting while typing.

  • in this case, you can use this same method in Edit’s Changetracking event

1

Use in the event onTyping Edit the section below that will solve for you:

  TThread.Queue(nil,
    procedure
    var
      txt, txt2: string;
      x: integer;
    begin
      txt := Edit1.Text;
      txt2 := '';
      for x := 0 to Length(txt) - 1 do
        if (txt.Chars[x] In ['0' .. '9']) then
          txt2 := txt2 + txt.Chars[x];

      txt := txt2;
      Edit1.Text := FormatFloat('#,##0.00', StrToFloatDef(txt, 0) / 100);
      Edit1.GoToTextEnd;
    end);

This code takes the content typed in Edit, removes the dots and commas (considering the numeric keyboard), then converts to the numeric format according to the language of the platform, in the Brazilian case, the number of 4 digits and 2 decimals would look like this 1.234,56

Browser other questions tagged

You are not signed in. Login or sign up in order to post.