Olá Pessoal,


Esses dicas um usuário me solicitou uma ajuda no sistema de consultoria para formatar valores no Delphi.  A princípio eu não tinha entendido direito pois temos os metodos formatfloat e a propriedade editmask dos TField mas a coisa ia mais além.


Ele queria simular o efeito que temos nas paginas dos banco, ir formatando os valores conforme vamos digitando. Pois bem entrei em um site e localizei a função javascript para fazer isso e entrei numa batalha para convertê-la para Delphi Language. Observe a função javascript:


function Formata(campo,tammax,teclapres,decimal) {
var tecla = teclapres.keyCode;
vr = Limpar(campo.value,"0123456789");
tam = vr.length;
dec=decimal
if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
if (tecla == 8 )
{ tam = tam - 1 ; }
if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
{
if ( tam <= dec )
{ campo.value = vr ; }
if ( (tam > dec) && (tam <= 5) ){
campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 6) && (tam <= 8) ){
campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
}
if ( (tam >= 9) && (tam <= 11) ){
campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 12) && (tam <= 14) ){
campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 15) && (tam <= 17) ){
campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
}}

Para quem vem de Delphi pode parecer meio louco, mas eu retirei algumas linhas, coloquei outras e aproveitei a ideia. No final temos isso aqui:


var
  s: String;
begin
   if (Key in [96..107]) or (Key in [48..57]) then
   begin
     S := Edit1.Text;
     S := StringReplace(S,',','',[rfReplaceAll]);
     S := StringReplace(S,'.','',[rfReplaceAll]);
     if Length(s) = 3 then
     begin
       s := Copy(s,1,1)+','+Copy(S,2);
       Edit1.Text := S;
       Edit1.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);
         Edit1.Text := s;
         Edit1.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);
           Edit1.Text := s;
           Edit1.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);
             Edit1.Text := s;
             Edit1.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);
               Edit1.Text := s;
               Edit1.SelStart := Length(S);
             end;
end;

O Código assusta um pouco no início mas faça o seguinte. Crie uma aplicação em Delphi, coloque um edit e no evento onKeyUp coloque o código acima. Rode  a aplicação e digite um valor qualquer.


Obvio que o código pode e deve ser melhorado mas já é um bom começo.


Abraços e até mais !!!