1

I have a textedit component and a button component. The button component will add the textedit component text value to a list if that value is greater than 0. On the textedit component I can as many decimal places as I want, but I'd like to validate two decimal places. Like If I put 00.0032 in the textedit component the validation will take that as 0. Is there a function that will allow me to do this or do I have to do this by my own code.

This is my code

if (Trim(textEdit.Text) <> '') and (StrToCurr(Trim(textEdit.Text)) <> 0) then
begin      
  code to add the value 
end;

1 Answers1

1

Reading your question two possible solutions come to my mind:

  1. You could convert to float multiply by 100 (to shift by two decimals) and round using floor:

    (Floor(StrToFloat(Trim(textEdit.Text)) * 100) <> 0)

    This performs a conversion to floating point which might be slow.

  2. An other solution could be to use string functions:

    (StrToCurr(Copy(textEdit.Text, 1, Pos('.', textEdit.Text) + 2)) <> 0)

    This copies the input string from beginning to two digits after the decimal separator '.'. Don't worry if your string is shorter (for example '0.1') you won't get an error.

Which solution is ultimately fast would have to be benchmarked.

Also have in mind, that not in every region a '.' is the decimal separator. In most of Europe for example decimal separator is ',' and thousands separator is '.'. Find out about TFormatSettings.

PS: You don't need to Trim before using StrToCurr because it does a trim internally.

Henning
  • 496
  • 4
  • 8