Determine if a string is numeric: Difference between revisions

Line 286:
isNumeric("0x10") = false
isNumeric("6b") = false</pre>
 
=={{header|Delphi}}==
This simple function is a wrapper around a built-in Delphi function
 
<lang Delphi>
function IsNumericString(const inStr: string): Boolean;
var
i: extended;
begin
Result := TryStrToFloat(inStr,i);
end;
</lang>
 
This console application tests the function:
 
<lang Delphi>
program isNumeric;
 
{$APPTYPE CONSOLE}
 
uses
Classes,
SysUtils;
 
function IsNumericString(const inStr: string): Boolean;
var
i: extended;
begin
Result := TryStrToFloat(inStr,i);
end;
 
 
{ Test function }
var
s: string;
c: Integer;
 
const
MAX_TRIES = 10;
sPROMPT = 'Enter a string (or type "quit" to exit):';
sIS = ' is numeric';
sISNOT = ' is NOT numeric';
 
begin
c := 0;
s := '';
repeat
Inc(c);
Writeln(sPROMPT);
Readln(s);
if (s <> '') then
begin
tmp.Add(s);
if IsNumericString(s) then
begin
Writeln(s+sIS);
end
else
begin
Writeln(s+sISNOT);
end;
Writeln('');
end;
until
(c >= MAX_TRIES) or (LowerCase(s) = 'quit');
 
end.
 
</lang>
 
Example summarised output:
 
<pre>
123 is numeric
-123.456 is numeric
-123.-456 is NOT numeric
.345 is numeric
m1k3 is NOT numeric
</pre>
 
=={{header|E}}==