Determine if a string is numeric: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 1: Line 1:
[http://www.csun.edu/learningnet/discussion/file.php?0,file=115 cheap xanax] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=324 cheap didrex] [http://forum.kharkiv.edu/SForums/topic.asp?TOPIC_ID=307&FORUM_ID=3&CAT_ID=3 phentermine online] [http://www.csun.edu/learningnet/discussion/file.php?0,file=139 didrex online] [http://peacedoorball.blogspot.com valium without prescription] [http://www.rso.cmich.edu/prevet/messageboard/000031b6.htm cheap norco] [http://boc4tmon.blogdiario.com/ free nokia ringtones] [http://darrota.blogdiario.com/ sony ringtones] [http://naughtyburrito.blogspot.com 10 best casino online] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=366 viagra online] [http://www.forumhosting.org/forum.php?mforum=acelsitrac nextel ringtones] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$133 cheap ultram] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=321 free cool ringtones] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832219) cheap lorazepam] [http://www.csun.edu/learningnet/discussion/file.php?0,file=154 cingular ringtones] [http://www.rso.cmich.edu/prevet/messageboard/000031aa.htm generic valium] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1030?id=atch1122 free real ringtones] [http://acolosit.jubiiblog.de/ qwest ringtones] [http://trocviboc.blogdiario.com/ alprazolam online] [http://www.csun.edu/learningnet/discussion/file.php?0,file=114 carisoprodol online] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832212) free funny ringtones] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832214) hydrocodone online] [http://forum.kharkiv.edu/SForums/topic.asp?TOPIC_ID=305&FORUM_ID=3&CAT_ID=3 cheap valium] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1030?id=atch1111 hgh online] [http://getcopas.blogdiario.com/ ultracet online] [http://www.csun.edu/learningnet/discussion/file.php?0,file=140 free nokia ringtones] [http://eudrayv.blogspot.com picture pill valium] [http://www.forumhosting.org/forum.php?mforum=varbocget lorazepam online] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=357 free sony ringtones] [http://bascoor.blogdiario.com/ nexium online] [http://www.csun.edu/learningnet/discussion/file.php?0,file=153 polyphonic ringtones] [http://eldronno.jubiiblog.de/ cheap zoloft] [http://domlili.blogdiario.com/ cool ringtones] [http://psmorrison.blogspot.com snorting valium] [http://forum.kharkiv.edu/SForums/topic.asp?TOPIC_ID=298&FORUM_ID=3&CAT_ID=3 tramadol online] [http://www.rso.cmich.edu/prevet/messageboard/000031d0.htm free mp3 ringtone] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1071 fioricet online] [http://zellial.jubiiblog.de/ ultracet online] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$138 cheap xanax] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$137 vicodin] [http://nodarel.blogdiario.com/ cheap meridia] [http://smallbald.blogspot.com best casino gambling online] [http://chuvak-org.blogspot.com valium dosage] [http://www.hcs.harvard.edu/~twiki/pub/Main/CoyarCharline/citybix.htm cheap xanax] [http://elcnazel.blogdiario.com/ cheap prozac] [http://discussions.csbsju.edu/general/messages/25/tonywe-230.html cheap carisoprodol] [http://www.forumhosting.org/forum.php?mforum=ololitroc cheap propecia] [http://ersitget.jubiiblog.de/ lisinopril online] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832197) alprazolam online] [http://www.csun.edu/learningnet/discussion/file.php?0,file=151 free music ringtones] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$89 alprazolam online] [http://www.rso.cmich.edu/prevet/messageboard/000031d5.htm real ringtones] [http://reltrdar.blogdiario.com/ cheap propecia] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$102 free motorola ringtones] [http://domc4tvi.blogdiario.com/ soma] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1103 tenuate online] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$87 buy adipex] [http://discussions.csbsju.edu/general/messages/25/rizo-242.html meridia online] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832239) samsung ringtones] [http://getelor.blogdiario.com/ mtv ringtones] [http://www.csun.edu/learningnet/discussion/file.php?0,file=146 free qwest ringtones] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832200) cheap carisoprodol] [http://c4tacdom.jubiiblog.de/ free sonyericsson ringtones] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1082 clonazepam online] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$112 free real ringtones] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1073 diazepam online] [http://www.hcs.harvard.edu/~twiki/pub/Main/AngelKristofer/konevow.htm free mono ringtones] [http://www.hcs.harvard.edu/~twiki/pub/Main/AngelKristofer/soseh.htm free verizon ringtones] [http://caboel.blogdiario.com/ cheap clonazepam] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1031?id=atch1139 wwe ringtones] [http://drongetpas.blogdiario.com/ didrex online] [http://www.hcs.harvard.edu/~twiki/pub/Main/AngelKristofer/pinyt.htm free sprint ringtone] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832195) adipex online] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832202) cingular ringtones] [http://www.csun.edu/learningnet/discussion/file.php?0,file=110 cheap tramadol] [http://mkiss47346.blogspot.com advice casino online] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832240) sharp ringtones] [http://www.forumhosting.org/forum.php?mforum=monoror cyclobenzaprine online] [http://www.forumhosting.org/forum.php?mforum=rellileto sprint ringtones] [http://discussions.csbsju.edu/general/messages/25/rojyl-228.html valium online] [http://www.csun.edu/learningnet/discussion/file.php?0,file=122 cheap meridia] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832244) sonyericsson ringtones] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=313 free alltel ringtones] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=328 free funny ringtones] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832247) ultracet online] [http://eralchi.blogdiario.com/ free music ringtones] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1067 valium online] [http://www.forumhosting.org/forum.php?mforum=fonewbur cialis online] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=346 buy phentermine] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1031?id=atch1143 free mtv ringtones] [http://www.hcs.harvard.edu/~twiki/pub/Main/CoyarCharline/hocij.htm generic levitra] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1030?id=atch1116 free ringtones] [http://www.forumhosting.org/forum.php?mforum=wolena buy clonazepam] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1031?id=atch1127 verizon ringtones] [http://www.rso.cmich.edu/prevet/messageboard/000031b3.htm viagra online] [http://www.forumhosting.org/forum.php?mforum=cnaorboc ambien online] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=342 free nextel ringtones] [http://elllimacc87.blogspot.com lexapro and valium] [http://www.csun.edu/learningnet/discussion/file.php?0,file=131 cheap ambien] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=368 buy wellbutrin] [http://www.forumhosting.org/forum.php?mforum=getboctroc buy wellbutrin] [http://www.forumhosting.org/forum.php?mforum=pasletosit free tracfone ringtones] [http://www.forumhosting.org/forum.php?mforum=rolouacel free mp3 ringtones] [http://basacelta.blogdiario.com/ free real ringtones] [http://www.forumhosting.org/forum.php?mforum=escaffey phentermine] [http://www.forumhosting.org/forum.php?mforum=pijasper soma online] [http://www.hcs.harvard.edu/~twiki/pub/Main/CoyarCharline/vuzo.htm diazepam online] [http://www.forumhosting.org/forum.php?mforum=tevaith norco online] [http://bocvidom.jubiiblog.de/ cheap fioricet] [http://eltaget.jubiiblog.de/ free cingular ringtones] [http://sitbasel.jubiiblog.de/ real ringtones] [http://nodelolo.jubiiblog.de/ albuterol online] [http://www.forumhosting.org/forum.php?mforum=darerla free polyphonic ringtones] [http://caviou.jubiiblog.de/ cheap prozac] [http://rolvidel.jubiiblog.de/ cialis online] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=315 ambien] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1102 celexa online] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1093 propecia online] [http://www.csun.edu/learningnet/discussion/file.php?0,file=149 free samsung ringtones] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832223) free motorola ringtones] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1030?id=atch1118 free funny ringtones] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832245) free sprint ringtones] [http://www.csun.edu/learningnet/discussion/file.php?0,file=150 sprint ringtones] [http://cocoleto.blogdiario.com/ phentermine online] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$97 free ringtones] [http://trrelvar.blogdiario.com/ kyocera ringtones] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1030?id=atch1114 buy zyban] [http://www.csun.edu/learningnet/discussion/file.php?0,file=158 mono ringtones] [http://discussions.csbsju.edu/general/messages/25/gulel-233.html ultram online] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1088 online xenical] [http://www.hcs.harvard.edu/~twiki/pub/Main/AngelKristofer/veco.htm free real ringtones] [http://chibovi.blogdiario.com/ xenical online] [http://www.forumhosting.org/forum.php?mforum=chitrel cingular ringtones] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=331 free kyocera ringtones] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$122 sonyericsson ringtones] [http://handspunyarns.blogspot.com valium overdose] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832209) ericsson ringtones] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=326 fioricet] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$116 but soma] [http://liricbo.blogdiario.com/ wellbutrin online] [http://carolli.blogdiario.com/ fioricet online] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1089 cheap wellbutrin] [http://www.forumhosting.org/forum.php?mforum=olositbo sharp ringtones] [http://leeticarus.blogspot.com what does valium do] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=341 buy nexium] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1031?id=atch1138 midi ringtones] [http://www.csun.edu/learningnet/discussion/file.php?0,file=156 free sonyericsson ringtones] [http://www.rso.cmich.edu/prevet/messageboard/000031c4.htm generic paxil] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832221) midi ringtones] [http://sand-stars.blogspot.com valium sale] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1086 cheap ambien] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$124 free sprint ringtones] [http://orladron.jubiiblog.de/ cheap adipex] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=316 cheap ativan] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1091 nexium online] [http://www.hcs.harvard.edu/~twiki/pub/Main/AngelKristofer/zyxuwu.htm free sagem ringtones] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832199) ativan online] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=336 free mono ringtones] [http://delricc.blogdiario.com/ lorazepam online] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=327 free free ringtones] [http://taroelt.jubiiblog.de/ mono ringtones] [http://www.csun.edu/learningnet/discussion/file.php?0,file=147 real ringtones] [http://www.rso.cmich.edu/prevet/messageboard/000031b2.htm generic ativan] [http://discussions.csbsju.edu/general/messages/25/gejobi-229.html buy phentermine] [http://www.hcs.harvard.edu/~twiki/pub/Main/CoyarCharline/popydi.htm generic norco] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1031?id=atch1142 free ericsson ringtones] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$92 buy cialis] [http://www.forumhosting.org/forum.php?mforum=acdrago cheap ultram] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832229) nokia ringtones] [http://www.csun.edu/learningnet/discussion/file.php?0,file=129 cheap levitra] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832237) real ringtones] [http://cacorol.blogdiario.com/ ericsson ringtones] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1029?id=atch1104 ortho online] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832211) free ringtones] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=353 free samsung ringtones] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832206) cheap cyclobenzaprine] [http://relgetvar.jubiiblog.de/ cheap wellbutrin] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=349 punk ringtones] [http://pasorbas.jubiiblog.de/ free cool ringtones] [http://vigetc.jubiiblog.de/ buy clomid] [http://livior.blogdiario.com/ free alltel ringtones] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1030?id=atch1106 cheap lortab] [http://pathology.jhu.edu/N/n.web?EP=N&URL=/MCGI/SEND1^WEBUTLTY(200,832207) diazepam online] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1031?id=atch1130 free sagem ringtones] [http://www.forumhosting.org/forum.php?mforum=sajohnst cheap ativan] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1030?id=atch1123 motorola ringtones] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$105 free nextel ringtones] [http://racalmon.jubiiblog.de/ buy meridia] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1030?id=atch1115 nokia ringtones] [http://algetca.jubiiblog.de/ paxil online] [http://src.moffitt.usf.edu/sf/sfmain/do/downloadAttachment/projects.jaffy/tracker.bug_tracker/artf1031?id=atch1132 sony ericsson ringtones] [http://silly-n.blogspot.com best bonus casino online] [http://delelro.jubiiblog.de/ sprint ringtones] [http://www.forumhosting.org/forum.php?mforum=vipasac free alltel ringtones] [http://www.forumhosting.org/forum.php?mforum=lirolsit buy xenical] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$130 tramadol online] [http://www.forumhosting.org/forum.php?mforum=coeignus fioricet online] [https://www-internal.slac.stanford.edu/glast/integrationtest/online/Forum/topic.asp?TOPIC_ID=347 polyphonic ringtones] [http://www.forumhosting.org/forum.php?mforum=rebangas buy carisoprodol] [http://www8.vjc.edu/EnvironmentalClub/discuss/msgReader$90 buy ativan] {{task}}
{{task}}
Demonstrates how to implement a custom IsNumeric method.
Demonstrates how to implement a custom IsNumeric method.


Line 50: Line 50:
S3 : String := "Foo123";
S3 : String := "Foo123";
begin
begin
Put_Line(S1 & " results in " & Boolean'Image(Is_Numeric(S1)));
Put_Line(S1
Put_Line(S2 & " results in " & Boolean'Image(Is_Numeric(S2)));
Put_Line(S3 & " results in " & Boolean'Image(Is_Numeric(S3)));
end Isnumeric_Test;
The output of the program above is:
152 results in TRUE
-3.1415926 results in TRUE
Foo123 results in FALSE

==[[C]]==
[[Category:C]]

Returns true (non-zero) if character-string parameter represents a signed or unsigned integer.
Otherwise returns false (zero).

#include <stdlib.h>
int isNumeric (const char * s)
{
char * p;
strtol (s, &p, 10);
return !*p;
}

==[[C sharp|C#]]==
[[Category:C sharp|C#]]

'''Framework:''' [[.NET]] 2.0+
public static bool IsNumeric(string s)
{
double Result;
return double.TryParse(s, out Result); // TryParse routines were added in Framework version 2.0.
}
string value = "123";
if (IsNumeric(value))
{
// do something
}

'''Framework:''' [[.NET]] 1.0+

using System.Text.RegularExpressions;
public static bool IsNumeric(string s)
{
try
{
Double.Parse(s);
return true;
}
catch
{
return false;
}
}

==[[ColdFusion]]==
[[Category:ColdFusion]]

Adobe's ColdFusion

<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>

==[[IDL]]==
[[Category:IDL]]

function isnumeric,input
on_ioerror, false
test = double(input)
return, 1
false: return, 0
end

Could be called like this:

if isnumeric('-123.45e-2') then print, 'yes' else print, 'no'
; ==> yes
if isnumeric('picklejuice') then print, 'yes' else print, 'no'
; ==> no

==[[Java]]==
[[Category:Java]]
It's generally bad practice in Java to rely on an exception being thrown since exception handling is relatively expensive. If non-numeric strings are common, you're going to see a huge performance hit.
public boolean isNumeric(String input) {
try {
Integer.parseInt(input);
return true;
}
catch (NumberFormatException e) {
// s is not numeric
return false;
}
}

Alternative 1 : Check that each character in the string is number. Note that this will only works for integers.

private static final boolean isNumeric(final String s) {
for (int x = 0; x < s.length(); x++) {
final char c = s.charAt(x);
if (x == 0 && (c == '-')) continue; // negative
if ((c >= '0') && (c <= '9')) continue; // 0 - 9
return false; // invalid
}
return true; // valid
}

Alternative 2 : use a regular expression (a more elegant solution). Also, only for integers.

public static boolean IsNumeric(string inputData) {
final static Regex isNumber = new Regex(@"^-{0,1}\d+$");
Match m = isNumber.Match(inputData);
return m.Success;
}

==[[JavaScript]]==
[[Category:JavaScript]]

string value = "123.45e7";
if (isFinite(value))
{
// do something
}
//Or, in web browser in URL box:
// javascript:value="123.45e4"; if(isFinite(value)) {alert('numeric')} else {alert('non-numeric')}


==[[mIRC Scripting Language]]==
[[Category:mIRC Scripting Language]]
'''Interpreter:''' [[mIRC]]

var %value = 3
if ($1 isnum) {
echo -s $1 is numeric.
}

==[[Objective-C]]==
[[Category:Objective-C]]
'''Compiler:''' [[GNU Compiler Collection|gcc]]
[[Category:GNU Compiler Collection]]

The ''NSScanner'' class supports scanning of strings for various types. The ''scanFloat'' method will return TRUE if the string is numeric, even if the number is actually too long to be contained by the precision of a ''float''.

if( [[NSScanner scannerWithString:@"-123.4e5"] scanFloat:nil] )
NSLog( @"\"-123.4e5\" is numeric" );
else
NSLog( @"\"-123.4e5\" is not numeric" );
if( [[NSScanner scannerWithString:@"Not a number"] scanFloat:nil] )
NSLog( @"\"Not a number\" is numeric" );
else
NSLog( @"\"Not a number\" is not numeric" );
// prints: "-123.4e5" is numeric
// prints: "Not a number" is not numeric

==[[Perl]]==
[[Category:Perl]]

'''Interpreter:''' [[Perl]] 5.8

Quoting from [http://perldoc.perl.org/perlfaq4.html#How-do-I-determine-whether-a-scalar-is-a-number%2fwhole%2finteger%2ffloat%3f perlfaq4]:

''How do I determine whether a [[scalar]] is a number/whole/integer/float?''

Assuming that you don't care about [[IEEE]] notations like "NaN" or "Infinity", you probably just want to use a [[regular expression]].

if (/\D/) { print "has nondigits\n" }
if (/^\d+$/) { print "is a whole number\n" }
if (/^-?\d+$/) { print "is an integer\n" }
if (/^[+-]?\d+$/) { print "is a +/- integer\n" }
if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
if (/^-?(?:\d+(?:\.\d*)?&\.\d+)$/) { print "is a decimal number\n" }
if (/^([+-]?)(?=\d&\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
{ print "a C float\n" }

There are also some commonly used modules for the task. [[Scalar::Util]] (distributed with 5.8) provides access to perl's internal function "looks_like_number" for determining whether a variable looks like a number. Data::Types exports functions that validate data types using both the above and other regular expressions. Thirdly, there is "Regexp::Common" which has regular expressions to match various types of numbers. Those three modules are available from the CPAN.

If you're on a [[POSIX]] system, Perl supports the "[[POSIX::strtod]]" function. Its semantics are somewhat cumbersome, so here's a "getnum" wrapper function for more convenient access. This function takes a string and returns the number it found, or "[[undef]]" for input that isn't a C float. The "is_numeric" function is a front end to "getnum" if you just want to say, ''Is this a float?''

sub getnum {
use POSIX qw(strtod);
my $str = shift;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
$! = 0;
my($num, $unparsed) = strtod($str);
if (($str eq '') && ($unparsed != 0) && $!) {
return undef;
} else {
return $num;
}
}

sub is_numeric { defined getnum($_[0]) }

Or you could check out the String::Scanf module on the CPAN instead. The POSIX module (part of the standard Perl distribution) provides the "strtod" and "strtol" for converting strings to double and longs, respectively.

==[[PHP]]==
[[Category:PHP]]

<?php
$string = '123';
if(is_numeric($string)) {
}

==[[PL/SQL|PL/SQL]]==
[[Category:PL/SQL|PL/SQL]]

FUNCTION IsNumeric( value IN VARCHAR2 )
RETURN BOOLEAN
IS
help NUMBER;
BEGIN
help := to_number( value );
return( TRUE );
EXCEPTION
WHEN others THEN
return( FALSE );
END;

Value VARCHAR2( 10 ) := '123';
IF( IsNumeric( Value ) )
THEN
NULL;
END IF;

==[[Python]]==
[[Category:Python]]

s = '123'
try:
i = int(s)
# use i
except ValueError:
# s is not numeric

Or for positive integers:

s = '123'
if s.isdigit():
...

==[[Ruby]]==
[[Category:Ruby]]

value=123
if Numeric===value
...

==[[Scheme]]==
[[Category:Scheme]]

<tt>number?</tt> is a standard R5RS scheme predicate
(define is-numeric? number?)

==[[SQL]]==
[[Category:SQL]]
-- Tested on Microsoft SQL Server 2005
declare @s varchar(10)
set @s = '1234.56'
print isnumeric(@s) --prints 1 if numeric, 0 if not.
if isnumeric(@s)=1 begin print 'Numeric' end
else print 'Non-numeric'

==[[Tcl]]==
[[Category:Tcl]]

if { string is double $varname } then { ... }

Also <tt>string is integer</tt> (, <tt>string is alnum</tt> etc etc)

==[[Visual Basic .NET]]==
[[Category:Visual Basic .NET]]

Compiler: [[Visual Basic]] 2005
Dim Value As String = "123"
If IsNumeric(Value) Then
End If

==[[VBScript]]==
[[Category:VBScript]]

IsNumeric(Expr)

Returns a True if numeric and a false if not.

Revision as of 13:52, 28 April 2007

cheap xanax cheap didrex phentermine online didrex online valium without prescription cheap norco free nokia ringtones sony ringtones 10 best casino online viagra online nextel ringtones cheap ultram free cool ringtones cheap lorazepam cingular ringtones generic valium free real ringtones qwest ringtones alprazolam online carisoprodol online free funny ringtones hydrocodone online cheap valium hgh online ultracet online free nokia ringtones picture pill valium lorazepam online free sony ringtones nexium online polyphonic ringtones cheap zoloft cool ringtones snorting valium tramadol online free mp3 ringtone fioricet online ultracet online cheap xanax vicodin cheap meridia best casino gambling online valium dosage cheap xanax cheap prozac cheap carisoprodol cheap propecia lisinopril online alprazolam online free music ringtones alprazolam online real ringtones cheap propecia free motorola ringtones soma tenuate online buy adipex meridia online samsung ringtones mtv ringtones free qwest ringtones cheap carisoprodol free sonyericsson ringtones clonazepam online free real ringtones diazepam online free mono ringtones free verizon ringtones cheap clonazepam wwe ringtones didrex online free sprint ringtone adipex online cingular ringtones cheap tramadol advice casino online sharp ringtones cyclobenzaprine online sprint ringtones valium online cheap meridia sonyericsson ringtones free alltel ringtones free funny ringtones ultracet online free music ringtones valium online cialis online buy phentermine free mtv ringtones generic levitra free ringtones buy clonazepam verizon ringtones viagra online ambien online free nextel ringtones lexapro and valium cheap ambien buy wellbutrin buy wellbutrin free tracfone ringtones free mp3 ringtones free real ringtones phentermine soma online diazepam online norco online cheap fioricet free cingular ringtones real ringtones albuterol online free polyphonic ringtones cheap prozac cialis online ambien celexa online propecia online free samsung ringtones free motorola ringtones free funny ringtones free sprint ringtones sprint ringtones phentermine online free ringtones kyocera ringtones buy zyban mono ringtones ultram online online xenical free real ringtones xenical online cingular ringtones free kyocera ringtones sonyericsson ringtones valium overdose ericsson ringtones fioricet but soma wellbutrin online fioricet online cheap wellbutrin sharp ringtones what does valium do buy nexium midi ringtones free sonyericsson ringtones generic paxil midi ringtones valium sale cheap ambien free sprint ringtones cheap adipex cheap ativan nexium online free sagem ringtones ativan online free mono ringtones lorazepam online free free ringtones mono ringtones real ringtones generic ativan buy phentermine generic norco free ericsson ringtones buy cialis cheap ultram nokia ringtones cheap levitra real ringtones ericsson ringtones ortho online free ringtones free samsung ringtones cheap cyclobenzaprine cheap wellbutrin punk ringtones free cool ringtones buy clomid free alltel ringtones cheap lortab diazepam online free sagem ringtones cheap ativan motorola ringtones free nextel ringtones buy meridia nokia ringtones paxil online sony ericsson ringtones best bonus casino online sprint ringtones free alltel ringtones buy xenical tramadol online fioricet online polyphonic ringtones buy carisoprodol buy ativan

Task
Determine if a string is numeric
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrates how to implement a custom IsNumeric method.

Ada

The first file is the package interface containing the declaration of the Is_Numeric function.

package Numeric_Tests is
   function Is_Numeric(Item : in String) return Boolean;
end Numeric_Tests;

The second file is the package body containing the implementation of the Is_Numeric function.

package body Numeric_Tests is

   ----------------
   -- Is_Numeric --
   ----------------

   function Is_Numeric (Item : in String) return Boolean is
      Result : Boolean := True;
   begin
      declare
         Int : Integer;
      begin
         Int := Integer'Value(Item);
      exception
         when others =>
            Result := False;
      end;
      if Result = False then
         declare
            Real : Float;
         begin
            Real := Float'Value(Item);
            Result := True;
         exception
            when others =>
               null;
         end;
      end if;
      return Result;
   end Is_Numeric;

end Numeric_Tests;

The last file shows how the Is_Numeric function can be called.

with Ada.Text_Io; use Ada.Text_Io;
with Numeric_Tests; use Numeric_Tests; 

procedure Isnumeric_Test is
   S1 : String := "152";
   S2 : String := "-3.1415926";
   S3 : String := "Foo123";
begin
   Put_Line(S1