Str2Word Function
Converts a string into a Word.

Unit
QESBPCSConvert

Declaration
Function Str2Word(const S: string): Word;

Description
Removes Thousand Separators if they are present as well as any leading or trailing white spaces (ie <= #32). If Number is Valid but out of Range then High (Word) will be returned for a greater value and 0 for a lesser value. Non-numeric will return 0.

Parameters
the String to process

Category
String/Integer Conversion Routines

Implementation

function Str2Word (const S: string): Word;
var
     S2: string;
     L: Int64;
     Error: Integer;
begin
     S2 := StripThousandSeparators (Trim (S)); // Remove Thousands Separators, if any
     try
          Val (S2, L, Error);
          if Error <> 0 then
               Result := 0 // Return 0 for non-numeric
          else if L > High (Word) then // Check with in boundaries
               Result := High (Word)
          else if L < Low (Word) then
               Result := Low (Word)
          else
               Result := L; // Return Value
     except
          Result := 0; // Return 0 for non-numeric
     end;
End;


HTML generated by Time2HELP
http://www.time2help.com