Determine if a string is numeric: Difference between revisions

m
no edit summary
(Undo spam)
mNo edit summary
Line 1:
[http://news.engin.brown.edu/forums/thread-view.asp?tid=196 buy hgh] [http://wc1.worldcrossing.com/WebX/.1de60a10 cheap didrex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=129 free sony ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=339 cheap ultram] [http://wc1.worldcrossing.com/WebX/.1de609ec hgh online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=85 free ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=156 fioricet online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=76 cialis online] [http://wc1.worldcrossing.com/WebX/.1de60a1e flexeril online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=336 carisoprodol online] [http://wc1.worldcrossing.com/WebX/.1de609ff real ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=198 cheap hoodia] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=165 cheap ambien] [http://news.engin.brown.edu/forums/thread-view.asp?tid=186 lipitor online] [http://wc1.worldcrossing.com/WebX/.1de609fa online pharmacy] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=88 free free ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=146 zanaflex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=153 cheap phentermine] [http://wc1.worldcrossing.com/WebX/.1de60a11 buy diethylpropion] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=240 soma online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=210 sprint ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e7 cheap diazepam] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=125 free sharp ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=170 hydrocodone online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=347 adipex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=155 xanax online] [http://wc1.worldcrossing.com/WebX/.1de609e3 clomid online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=356 tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=222 sharp ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=98 lisinopril online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=223 wellbutrin online] [http://wc1.worldcrossing.com/WebX/.1de60a15 free kyocera ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=362 motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=115 cheap phentermine] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=215 zoloft online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=167 carisoprodol online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=164 buy alprazolam] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=177 diethylpropion online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=200 nokia ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=69 albuterol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=357 free funny ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=334 valium online] [http://wc1.worldcrossing.com/WebX/.1de60a29 tramadol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=338 fioricet] [http://wc1.worldcrossing.com/WebX/.1de60a09 cheap viagra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=124 free samsung ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=198 mtv ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=164 cheap norco] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=199 music ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=166 ativan online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=188 cheap tenuate] [http://wc1.worldcrossing.com/WebX/.1de60a17 buy lipitor] [http://wc1.worldcrossing.com/WebX/.1de60a12 free ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=151 order soma] [http://wc1.worldcrossing.com/WebX/.1de609df ambien online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=231 rivotril online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=343 cialis] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=134 tramadol online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=227 ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=228 free mtv ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=177 propecia online] [http://wc1.worldcrossing.com/WebX/.1de60a14 jazz ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=187 free kyocera ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=345 cheap viagra] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=348 but norco] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=162 alltel ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=193 zanaflex online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=196 free motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=139 viagra online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=191 lortab online] [http://wc1.worldcrossing.com/WebX/.1de60a1b free music ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a19 mp3 ringtones] [http://wc1.worldcrossing.com/WebX/.1de609dc buy albuterol] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=351 buy clonazepam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=171 ambien online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=173 xenical online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=121 free real ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a20 cheap nexium] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=142 online wellbutrin] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=130 sonyericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0e celexa online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=158 diazepam online] [http://wc1.worldcrossing.com/WebX/.1de609f7 nextel ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a28 cheap soma] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=197 mp3 ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=184 hoodia] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=359 free nextel ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=165 vicodin online] [http://wc1.worldcrossing.com/WebX/.1de60a01 sagem ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=217 free punk ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=117 order propecia] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=195 mono ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=116 free polyphonic ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=96 cheap levitra] [http://news.engin.brown.edu/forums/thread-view.asp?tid=224 wwe ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f0 cheap lorazepam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=209 samsung ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a31 zanaflex online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=203 norco online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=178 buy nexium] [http://news.engin.brown.edu/forums/thread-view.asp?tid=157 cheap ultram] [http://wc1.worldcrossing.com/WebX/.1de609ed buy hoodia] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=183 hgh online] [http://wc1.worldcrossing.com/WebX/.1de609e5 free cool ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=103 free mono ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a00 rivotril online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=101 meridia online] [http://wc1.worldcrossing.com/WebX/.1de60a0f cheap cialis] [http://news.engin.brown.edu/forums/thread-view.asp?tid=229 jazz ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=78 clomid] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=112 ortho] [http://wc1.worldcrossing.com/WebX/.1de609e0 ativan online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=342 diazepam online] [http://wc1.worldcrossing.com/WebX/.1de60a1d cingular ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=229 valium online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=92 hydrocodone online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=344 online ativan] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=224 sagem ringtones] [http://wc1.worldcrossing.com/WebX/.1de609de cheap alprazolam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=195 rivotril online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=77 cingular ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=152 valium online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=235 tenuate] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=99 lorazepam online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=106 free mtv ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a04 free sonyericsson ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=136 cheap ultram] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=148 zyban online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=204 mp3 ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=239 cheap sildenafil] [http://news.engin.brown.edu/forums/thread-view.asp?tid=219 alltel ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=123 free sagem ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=147 zoloft online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=172 but clonazepam] [http://wc1.worldcrossing.com/WebX/.1de609f1 lortab online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=361 real ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e6 buy cyclobenzaprine] [http://wc1.worldcrossing.com/WebX/.1de60a33 prozac online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=227 ultracet online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=183 albuterol] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=222 cheap vigrx] [http://wc1.worldcrossing.com/WebX/.1de609fb ortho online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=178 free ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de609db but adipex] [http://wc1.worldcrossing.com/WebX/.1de60a18 free motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=107 music ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a35 verizon ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=141 vigrx online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=225 free wwe ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a30 xenical online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=168 paxil online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=208 cheap phentermine] [http://wc1.worldcrossing.com/WebX/.1de609ea free ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=127 soma online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=84 cheap diethylpropion] [http://wc1.worldcrossing.com/WebX/.1de609fd punk ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=346 meridia online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=354 free nokia ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=355 free free ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=350 online alprazolam] [http://news.engin.brown.edu/forums/thread-view.asp?tid=181 cheap sildenafil] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=86 fioricet online] [http://wc1.worldcrossing.com/WebX/.1de60a13 order fioricet] [http://news.engin.brown.edu/forums/thread-view.asp?tid=162 order meridia] [http://news.engin.brown.edu/forums/thread-view.asp?tid=202 tracfone ringtones] [http://wc1.worldcrossing.com/WebX/.1de609ee but hydrocodone] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=131 sprint ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=226 free sony ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=238 free sharp ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=216 free sonyericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=189 ortho online] [http://wc1.worldcrossing.com/WebX/.1de60a16 levitra] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=213 prozac online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=199 cheap zyban] [http://wc1.worldcrossing.com/WebX/.1de60a26 free sharp ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=202 free nokia ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f4 mono ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=122 rivotril online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=94 jazz ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=190 cheap lisinopril] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=111 norco online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=73 cheap ativan] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=95 free kyocera ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=169 cheap levitra] [http://wc1.worldcrossing.com/WebX/.1de60a2e xanax online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=221 cool ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=169 online cialis] [http://news.engin.brown.edu/forums/thread-view.asp?tid=179 didrex online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=363 free samsung ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=335 phentermine online] [http://wc1.worldcrossing.com/WebX/.1de60a36 zyban online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=175 diazepam online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=91 cheap hoodia] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=89 free funny ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=205 nextel ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=159 cialis online] [http://wc1.worldcrossing.com/WebX/.1de60a0c wellbutrin online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=187 cheap celexa] [http://wc1.worldcrossing.com/WebX/.1de60a06 order tenuate] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=138 verizon ringtones] {{task}}
{{task}}
Demonstrates how to implement a custom IsNumeric method.
 
Line 50:
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]]==
[[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.
Anonymous user