I have an array of strings containing integer numbers. I need to convert them into integers where possible.
Therefore I do like:
if not TryStrToInt ( grid.Cells[columnIndex, i], integerValue ) then begin
errorsCount := errorsCount + 1;
errMemo.Lines.Add ( 'Column "' + fstColumn.Name + '" Line ' + IntTostr ( i ) + ' Value "' + grid.Cells[columnIndex, i] + '" must be integer.' );
end
else begin
{deal with integerValue}
end;
But when TryStrToInt faces number like '10.0', '11.00' etc, which actually an Integer, it returns false which proceeds error. TryStrToInt in SysUtils.pas implemented as:
function TryStrToInt(const S: string; out Value: Integer): Boolean;
var
E: Integer;
begin
Val(S, Value, E);
Result := E = 0;
end;
As any other string-number convertions it uses Val.
I see only one bad solution like try to convert string to float and then, if successfully, convert float to integer. But it seems ugly. Is there any other standard way? May be not using Val.
UPD: I use Delphi XE5.
10.0is not an Integer. You might want to useTryStrToFloator parse the string yourself. it not that difficult.Fracfunction to check if it's zero. what is "ugly" about this? doesn't phpintvalreturns10for10.5?