Count the total number of decimal places in Delphi

0

I need to count the number of decimal places in Delphi (10.1) in a string, I'll use Edit, validating monetary values when typing, before inserting in the edit.

I can validate if it is a valid Currency, but when comparing it it means 1.23 equal 1,230. Of course, it is valid.

I know there is a rounding option, but it is not my goal at this stage, but merely to count the number of decimal places in a string, so as to combine with other functions , prevent typing of more houses, or characters that are not part of a valid currency, including more zeros after 1.23.

I researched a lot, and at the moment I got the formula below but I do not think it's the best idea.

function ContarDecimaisTESTE(S: string):string;
//necessita verificar se é um (float,currency) valido antes.
var
  Saida: boolean;
  i,j, k: word;
  w: string;
  n: Integer;
begin
  k:= 0;
  Saida:= false;
  n:= length(s);
  i:= n;
  j:= 0;

  for I := 1 to n do
      begin
        w:=Copy(S,i,1);
        j:=j+1;
        if (w = ',') then
          begin
            Saida:=True;
            Break;
          end;
      end;

  if (Saida=True) then
    begin
      k:=(n-j);
    end else
      begin
        k:=0;
      end;
  Result:=IntToStr(k);

end;

I came up with something better, much like the idea of Junior, but I used more codes:

function TamanhoCasasDec(S:string):Int64;//Teste
var
  a,b:integer;
  c:string;
begin
  a:=length(S);
  b:=pos(',',S);

  try
    c:=Copy(FilterChars(S,[',']),1,1);//copiar a primeira virgula, se houver
  except
    //
  end;

  if (c = ',') then
    begin
      Result:=Length(Trim(copy(S, b + 1, a - b)));
    end else
      begin
        Result:=0;
      end;

end;
    
asked by anonymous 26.06.2018 / 21:24

1 answer

0

In case it would not just tell the separator to the end of the string?

Something like:

function Obter_QtdCasasDecimais(aValor: String): Integer;
begin
  Result := Length(Copy(aValor, Pos(',', aValor)+1, Length(aValor)));
end;

If it is this is simple

    
26.06.2018 / 22:12