This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,28 @@
#include <sstream> // for istringstream
using namespace std;
bool isNumeric( const char* pszInput, int nNumberBase )
{
istringstream iss( pszInput );
if ( nNumberBase == 10 )
{
double dTestSink;
iss >> dTestSink;
}
else if ( nNumberBase == 8 || nNumberBase == 16 )
{
int nTestSink;
iss >> ( ( nNumberBase == 8 ) ? oct : hex ) >> nTestSink;
}
else
return false;
// was any input successfully consumed/converted?
if ( ! iss )
return false;
// was all the input successfully consumed/converted?
return ( iss.rdbuf()->in_avail() == 0 );
}

View file

@ -0,0 +1,7 @@
bool isNumeric( const char* pszInput, int nNumberBase )
{
string base = "0123456789ABCDEF";
string input = pszInput;
return (input.find_first_not_of(base.substr(0, nNumberBase)) == string::npos);
}

View file

@ -0,0 +1,3 @@
bool isNumeric(const std::string& input) {
return std::all_of(input.begin(), input.end(), ::isdigit);
}

View file

@ -0,0 +1,15 @@
<cfset TestValue=34>
TestValue: <cfoutput>#TestValue#</cfoutput><br>
<cfif isNumeric(TestValue)>
is Numeric.
<cfelse>
is NOT Numeric.
</cfif>
<cfset TestValue="NAS">
TestValue: <cfoutput>#TestValue#</cfoutput><br>
<cfif isNumeric(TestValue)>
is Numeric.
<cfelse>
is NOT Numeric.
</cfif>

View file

@ -0,0 +1,3 @@
(defun numeric-string-p (string)
(let ((*read-eval* nil))
(ignore-errors (numberp (read-from-string string)))))

View file

@ -0,0 +1,5 @@
(defun numeric-string-p (string)
(handler-case (progn (parse-number:parse-number string)
t) ; parse succeeded, discard it and return true (t)
(parse-number::invalid-number ()
nil))) ; parse failed, return false (nil)

View file

@ -0,0 +1,9 @@
import std.stdio, std.string, std.array;
void main() {
foreach (const s; ["12", " 12\t", "hello12", "-12", "02"
"0-12", "+12", "1.5", "1,000", "1_000",
"0x10", "0b10101111_11110000_11110000_00110011",
"-0b10101", "0x10.5"])
writefln(`isNumeric("%s"): %s`, s, s.strip().isNumeric(true));
}

View file

@ -0,0 +1,27 @@
import std.stdio, std.string, std.conv, std.array, std.exception;
bool isNumeric(in string s) /*pure*/ {
const s2 = s.strip().toLower().replace("_", "").replace(",", "");
try {
s2.to!real();
} catch (ConvException e) {
if (s2.startsWith("0x"))
return !to!ulong(s2[2 .. $], 16)
.collectException!ConvException();
else if (s2.startsWith("0b"))
return !to!ulong(s2[2 .. $], 2)
.collectException!ConvException();
else
return false;
}
return true;
}
void main() {
foreach (const s; ["12", " 12\t", "hello12", "-12", "02"
"0-12", "+12", "1.5", "1,000", "1_000",
"0x10", "0b10101111_11110000_11110000_00110011",
"-0b10101", "0x10.5"])
writefln(`isNumeric("%s"): %s`, s, isNumeric(s));
}

View file

@ -0,0 +1,6 @@
function IsNumericString(const inStr: string): Boolean;
var
i: extended;
begin
Result := TryStrToFloat(inStr,i);
end;

View file

@ -0,0 +1,51 @@
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.

View file

@ -0,0 +1,8 @@
def isNumeric(specimen :String) {
try {
<import:java.lang.makeDouble>.valueOf(specimen)
return true
} catch _ {
return false
}
}

View file

@ -0,0 +1,7 @@
include get.e
function is_numeric(sequence s)
sequence val
val = value(s)
return val[1]=GET_SUCCESS and atom(val[2])
end function