Sign in Join
FPDoc Onlineexperiment

StrToIntDef function

In unit sysutils

Convert a string to an integer value, with a default value.

Declaration

pascal
function StrToIntDef(const S: string; Default: LongInt): LongInt

Description

StrToIntDef will convert a string to an integer. If the string contains invalid characters or has an invalid format, then Default is returned.

To be successfully converted, a string can contain a combination of numerical characters, possibly preceded by a minus sign (-). Spaces are not allowed.

Errors

None.

See also

Example: ex83

pascal
Program Example82;

{$mode objfpc}

{ This program demonstrates the StrToInt function }

Uses sysutils;

Begin
  Writeln (StrToIntDef('1234',0));
  Writeln (StrToIntDef('-1234',0));
  Writeln (StrToIntDef('0',0));
  Try
    Writeln (StrToIntDef('12345678901234567890',0));
  except
    On E : EConvertError do
      Writeln ('Invalid number encountered');
  end;
End.

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.