Formatar Edit com unidades
Bom dia, nas minhas buscas sempre encontro uma forma de formatar o campo Edit com valores tipo moeda. Mas não encontro algo que faça com que o usuário apenas digite o número e o próprio Edit formate ele da seguinte forma:
usuário digita 1500000 por exemplo automaticamente o Edit.text adicione os '.' (pontos) nos locais certos, tipo, 1.500.000
Alguém pode me ajudar?
Desde já, muito obrigado!
usuário digita 1500000 por exemplo automaticamente o Edit.text adicione os '.' (pontos) nos locais certos, tipo, 1.500.000
Alguém pode me ajudar?
Desde já, muito obrigado!
Helton Ferreira
Curtidas 0
Melhor post
Marcos Saffran
19/10/2013
Lampião,
no evento onkeyup do edit coloque o código abaixo:
no evento onkeyup do edit coloque o código abaixo:
procedure TFormContrato.edValorTotalKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var s: String;
begin
if (Key in [96..107]) or (Key in [48..57]) then
begin
S := edValorTotal.Text;
S := StringReplace(S,',','',[rfReplaceAll]);
S := StringReplace(S,'.','',[rfReplaceAll]);
if Length(s) = 3 then
begin
s := Copy(s,1,1)+','+Copy(S,2,15);
edValorTotal.Text := S;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) > 3) and (Length(s) < 6) then
begin
s := Copy(s,1,length(s)-2)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) >= 6) and (Length(s) < 9) then
begin
s := Copy(s,1,length(s)-5)+'.'+Copy(s,length(s)-4,3)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) >= 9) and (Length(s) < 12) then
begin
s := Copy(s,1,length(s)-8)+'.'+Copy(s,length(s)-7,3)+'.'+
Copy(s,length(s)-4,3)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) >= 12) and (Length(s) < 15) then
begin
s := Copy(s,1,length(s)-11)+'.'+Copy(s,length(s)-10,3)+'.'+
Copy(s,length(s)-7,3)+'.'+Copy(s,length(s)-4,3)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end;
end;
end;GOSTEI 1
Mais Respostas
Marcio
19/10/2013
é muito simples cara!
var
valor: double;
begin
valor := StrToCurr(Edit1.Text);
Edit1.Text := FormatFloat('#,.##', valor)
GOSTEI 0
Helton Ferreira
19/10/2013
Prezado Marcio,
Acredito que não seja exatamente isso o que to querendo. Pois o usuário poderá digitar qualquer valor entre, por exemplo 50.000 à 2.000.000. E preciso que no exato momento que o usuário estiver digitando o valor, por exemplo ele vai colocar 1500000, apareça os pontos automaticamente, sem que o usuário precise adicionar o ponto.
Acredito que não seja exatamente isso o que to querendo. Pois o usuário poderá digitar qualquer valor entre, por exemplo 50.000 à 2.000.000. E preciso que no exato momento que o usuário estiver digitando o valor, por exemplo ele vai colocar 1500000, apareça os pontos automaticamente, sem que o usuário precise adicionar o ponto.
GOSTEI 0
Julio Mendonça
19/10/2013
Mano tu é um deus, obrigado :)
Lampião,
no evento onkeyup do edit coloque o código abaixo:
no evento onkeyup do edit coloque o código abaixo:
procedure TFormContrato.edValorTotalKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var s: String;
begin
if (Key in [96..107]) or (Key in [48..57]) then
begin
S := edValorTotal.Text;
S := StringReplace(S,',','',[rfReplaceAll]);
S := StringReplace(S,'.','',[rfReplaceAll]);
if Length(s) = 3 then
begin
s := Copy(s,1,1)+','+Copy(S,2,15);
edValorTotal.Text := S;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) > 3) and (Length(s) < 6) then
begin
s := Copy(s,1,length(s)-2)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) >= 6) and (Length(s) < 9) then
begin
s := Copy(s,1,length(s)-5)+'.'+Copy(s,length(s)-4,3)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) >= 9) and (Length(s) < 12) then
begin
s := Copy(s,1,length(s)-8)+'.'+Copy(s,length(s)-7,3)+'.'+
Copy(s,length(s)-4,3)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) >= 12) and (Length(s) < 15) then
begin
s := Copy(s,1,length(s)-11)+'.'+Copy(s,length(s)-10,3)+'.'+
Copy(s,length(s)-7,3)+'.'+Copy(s,length(s)-4,3)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end;
end;
end;GOSTEI 0
Jefferson
19/10/2013
Lampião,
no evento onkeyup do edit coloque o código abaixo:
no evento onkeyup do edit coloque o código abaixo:
procedure TFormContrato.edValorTotalKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var s: String;
begin
if (Key in [96..107]) or (Key in [48..57]) then
begin
S := edValorTotal.Text;
S := StringReplace(S,',','',[rfReplaceAll]);
S := StringReplace(S,'.','',[rfReplaceAll]);
if Length(s) = 3 then
begin
s := Copy(s,1,1)+','+Copy(S,2,15);
edValorTotal.Text := S;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) > 3) and (Length(s) < 6) then
begin
s := Copy(s,1,length(s)-2)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) >= 6) and (Length(s) < 9) then
begin
s := Copy(s,1,length(s)-5)+'.'+Copy(s,length(s)-4,3)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) >= 9) and (Length(s) < 12) then
begin
s := Copy(s,1,length(s)-8)+'.'+Copy(s,length(s)-7,3)+'.'+
Copy(s,length(s)-4,3)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end
else
if (Length(s) >= 12) and (Length(s) < 15) then
begin
s := Copy(s,1,length(s)-11)+'.'+Copy(s,length(s)-10,3)+'.'+
Copy(s,length(s)-7,3)+'.'+Copy(s,length(s)-4,3)+','+Copy(S,length(s)-1,15);
edValorTotal.Text := s;
edValorTotal.SelStart := Length(S);
end;
end;
end;nice Lampião!!!
GOSTEI 0