Edit Currency Delphi Firemonkey

5

I need to format an edit in the format 0,00 in FireMonkey , preferably using the ChangeTracking event. I tried using the following procedure that did not work.

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;
    
asked by anonymous 11.11.2017 / 20:28

1 answer

4

The method for formatting follows:

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

I do not advise you to use this guy in ChangeTracking , the cool thing is you shoot this method when the guy finishes filling the field.

Here is an example usage:

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: I could implement a key lock in the edit, if it is "NumbersOnly"

    
11.11.2017 / 23:59