Determine if a string is numeric: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(revert spam)
Line 1: Line 1:
{{task}}
[http://wc1.worldcrossing.com/WebX/.1de60a19 free mp3 ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=786 wellbutrin online] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=824 free music ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=75 cheap rivotril] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=69 lorazepam online] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=117 punk ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/wellbutrin_1082.htm wellbutrin online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3724 sprint ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e7 cheap diazepam] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30240 carisoprodol online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=107 free music ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2873 cheap nexium] [http://students.hsc.unt.edu/housing/item.cfm?type=2887 pharmacy online online] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=89 vigrx online] [http://students.hsc.unt.edu/housing/item.cfm?type=2890 zanaflex online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=180 online flexeril] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=201 free nextel ringtones] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=38 buy paxil] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=811 hoodia online] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=113 sonyericsson ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2848 cheap soma] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/realringtones_1857.htm real ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3756 but lortab] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=81 free music ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=198 mtv ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1355 but clonazepam] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=103 qwest ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3751 sildenafil online] [http://wc1.worldcrossing.com/WebX/.1de60a29 tramadol online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3702 paxil online] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=95 hoodia online] [http://wc1.worldcrossing.com/WebX/.1de60a04 free sonyericsson ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=793 cheap sildenafil] [http://students.hsc.unt.edu/housing/item.cfm?type=2871 cheap wellbutrin] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=46 tramadol online] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=82 free verizon ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2923 sony ringtones] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=68 ambien online] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=47 cheap soma] [http://library.cshl.edu/wp/vb/member.php?u=1374 free qwest ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30245 cialis] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30346 free music ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a06 cheap tenuate] [http://students.hsc.unt.edu/housing/item.cfm?type=2855 buy diazepam] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3730 punk ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=177 diethylpropion online] [http://students.hsc.unt.edu/housing/item.cfm?type=2907 free sprint ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/vicodin_8272.htm vicodin online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=141 buy vigrx] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/cialis_874.htm cheap cialis] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=162 free alltel ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=818 nextel ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2878 cheap sildenafil] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/norco_532.htm norco] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=113 paxil online] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=76 free nextel ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a01 sagem ringtones] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=25 buy xanax] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=230 free verizon ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3740 cheap cialis] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=111 free cingular ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3738 online xanax] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=770 cheap diazepam] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30327 buy zyban] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=48 paxil online] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/polyphonicringtones_8614.htm free polyphonic ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=144 xanax online] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/cingularringtones_7013.htm free cingular ringtones] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=48 valium] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=821 motorola ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=41 viagra online] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=71 free ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e5 free cool ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=843 free jazz ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=769 cheap ultram] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=80 nokia ringtones] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=55 diazepam online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30351 free sonyericsson ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=71 cheap lortab] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30333 free funny ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=216 zyban online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=234 sprint ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30264 hydrocodone] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30366 jazz ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30230 adipex] [http://students.hsc.unt.edu/housing/item.cfm?type=2870 cheap xenical] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=82 buy diazepam] [http://wc1.worldcrossing.com/WebX/.1de60a32 zoloft online] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/tracfoneringtones_5328.htm tracfone ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=800 cheap tenuate] [http://wc1.worldcrossing.com/WebX/.1de609f4 free mono ringtones] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=68 cheap zyban] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=227 ultracet online] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=88 mono ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30347 verizon ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=773 viagra online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=168 celexa online] [http://library.cshl.edu/wp/vb/member.php?u=1376 motorola ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3699 meridia online] [http://wc1.worldcrossing.com/WebX/.1de60a03 sony ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=45 vicodin] [http://students.hsc.unt.edu/housing/item.cfm?type=2854 but ultram] [http://students.hsc.unt.edu/housing/item.cfm?type=2860 cheap adipex] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3711 cheap lisinopril] [http://wc1.worldcrossing.com/WebX/.1de609eb free funny ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3773 kyocera ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=164 alprazolam online] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=816 funny ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=221 order vicodin] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=99 lorazepam online] [http://wc1.worldcrossing.com/WebX/.1de609ec hgh online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3765 samsung ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/carisoprodol_1193.htm cheap carisoprodol] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/lorazepam_17.htm cheap lorazepam] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=822 samsung ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/fioricet_7129.htm order fioricet] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30318 cheap diethylpropion] [http://library.cshl.edu/wp/vb/member.php?u=1370 tracfone ringtones] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=65 paxil] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=79 clomid online] [http://wc1.worldcrossing.com/WebX/.1de60a14 jazz ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/clonazepam_4321.htm clonazepam online] [http://library.cshl.edu/wp/vb/member.php?u=1375 free real ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=838 free wwe ringtones] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=35 vicodin online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=193 meridia online] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=45 cyclobenzaprine] [http://library.cshl.edu/wp/vb/member.php?u=1365 ultracet online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3695 carisoprodol online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=142 wellbutrin online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3700 norco] [http://library.cshl.edu/wp/vb/member.php?u=1380 verizon ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e0 ativan online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30278 cheap nexium] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=206 pharmacy online online] [http://wc1.worldcrossing.com/WebX/.1de60a23 polyphonic ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3754 celexa online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=129 free sony ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2882 cheap zoloft] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3775 jazz ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2916 free alltel ringtones] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=53 but fioricet] [http://wc1.worldcrossing.com/WebX/.1de609de but alprazolam] [http://wc1.worldcrossing.com/WebX/.1de60a0c wellbutrin online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3729 mono ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3697 cheap diazepam] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3715 cheap vigrx] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30348 free polyphonic ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=69 ortho online] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=87 pharmacy online online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=106 mtv ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e4 clonazepam online] [http://students.hsc.unt.edu/housing/item.cfm?type=2904 free real ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=84 free mp3 ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2919 sharp ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3703 hydrocodone online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=97 cheap lipitor] [http://library.cshl.edu/wp/vb/member.php?u=1342 phentermine online] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3761 free ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0b vigrx online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=130 sonyericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de609fe qwest ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=74 cheap carisoprodol] [http://students.hsc.unt.edu/housing/item.cfm?type=2876 didrex online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30280 propecia online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30364 free ericsson ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=94 free cingular ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=780 paxil online] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=806 cheap diethylpropion] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=796 lisinopril online] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=799 celexa online] [http://kc.vanderbilt.edu/forums/toast.asp?sub=show&action=posts&fid=5&tid=62 cheap vicodin] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/sprintringtones_2715.htm sprint ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=69 albuterol online] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=829 sonyericsson ringtones] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=15&tid=3745 buy levitra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=143 wwe ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=44 norco online] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/phentermine_8208.htm cheap phentermine] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=78 online clomid] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=97 free ericsson ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=43 adipex online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=118 buy prozac] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=109 free nextel ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30359 free wwe ringtones] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=835 cool ringtones] [http://www.colorado.edu/studentgroups/backcountryclub/blahdocs/uploads/hydrocodone_3335.htm hydrocodone online] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=32 meridia online] [http://wc1.worldcrossing.com/WebX/.1de60a24 propecia online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=194 midi ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0d wwe ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=165 but ambien] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=813 free nokia ringtones] [http://www4.nau.edu/fera/index.asp?sub=show&action=posts&fid=4&tid=52 lorazepam online] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=56 online lipitor] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30353 mono ringtones] [http://ae.siam.edu/forums/toast.asp?sub=show&action=posts&fid=2&tid=48 order propecia] [http://www.ovu.edu/community/toast.asp?sub=show&action=posts&fid=5&tid=3714 cheap pharmacy online] [http://www.aswsu-ddp.wsu.edu/toast/forums.asp?sub=show&action=posts&fid=3&tid=831 sony ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a07 free tracfone ringtones] {{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
Put_Line(S1 & " results in " & Boolean'Image(Is_Numeric(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

==[[Toka]]==
[[Category:Toka]]

Returns TRUE (-1) if character-string parameter represents a signed
or unsigned integer. Otherwise returns FALSE (zero).

[ ( string -- flag )
>number nip ] is isNumeric

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

IsNumeric(Expr)

Returns a True if numeric and a false if not.

Revision as of 09:59, 22 July 2007

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 & " results in " & Boolean'Image(Is_Numeric(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

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#

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

Adobe's ColdFusion

<cfset TestValue=34>
  TestValue: <cfoutput>#TestValue#</cfoutput>
<cfif isNumeric(TestValue)> is Numeric. <cfelse> is NOT Numeric. </cfif>
<cfset TestValue="NAS">
  TestValue: <cfoutput>#TestValue#</cfoutput>
<cfif isNumeric(TestValue)> is Numeric. <cfelse> is NOT Numeric. </cfif>

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

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

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

Interpreter: mIRC

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

Objective-C

Compiler: gcc

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

Interpreter: Perl 5.8

Quoting from 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. 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 "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

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

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

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

Or for positive integers:

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

Ruby

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

Scheme

number? is a standard R5RS scheme predicate

 (define is-numeric? number?)

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

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

Also string is integer (, string is alnum etc etc)

Visual Basic .NET

Compiler: Visual Basic 2005

Dim Value As String = "123"
If IsNumeric(Value) Then
    
End If

Toka

Returns TRUE (-1) if character-string parameter represents a signed or unsigned integer. Otherwise returns FALSE (zero).

[ ( string -- flag )
  >number nip ] is isNumeric

VBScript

IsNumeric(Expr)

Returns a True if numeric and a false if not.