Buscar

Função para alinhar um número a direita formatando as casas decimais

Código

//essa função deve ser chamada no evento KeyPress do objeto.
//Text -> edit1.Text
//Key -> Key,, tecla precionada
//Espaco -> tamanho final da string
//Decimal -> casas decimais da string
Exemplo de como essa função deve ser chamada:
if (Key in ['0'..'9', #8]) then
    Edit1.Text := fAlinhaD( Edit1.Text, Key, '12', '2' );

Function fAlinhaD( const Texto, Key, Espaco, Decimal : String ): String;
Var
vChar, vDiv : String;
I : Integer;
begin
  vDiv := '1';
  For I := 1 to StrToInt( Decimal )do
    vDiv := vDiv + '0';
  vChar := IfThen( Key = #8, copy( Texto, 1, length( Texto )- 1 ), Texto + Key );
  While ( pos( ',', vChar ) > 0 ) or ( pos( '.', vChar ) > 0 ) do
    begin
      Delete( vChar, pos( '.', vChar ), 1 );
      Delete( vChar, pos( ',', vChar ), 1 );
    end;
    if vChar = '' Then
      vChar := '0';
  Result := format('%'+Espaco+'.'+Decimal+'n',[strtofloat( vChar )/StrToInt( vDiv )]);
End;
 

Publicidade

Vote na dica




Quantidade de votos: 0 votos
Aceitação: 0%


Detalhes da dica

Categoria: Componentes
Adicionada dia: 23/04/07
Por: Jurandi Nunes Da Silva
Visualizada: 4622 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009