View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > IsNumeric PreviousNext

IsNumeric

Determines whether a string can be converted to a numeric value. Supports numbers in U.S. number format. For other number support, use LSIsNumeric.

True, if string can be converted to a number; otherwise, False.

Decision functions

IsNumeric(string)

IsBinary

Parameter

Description

string

A string or a variable that contains one.

<h3>IsNumeric Example</h3>
<cfif IsDefined("FORM.theTestValue")>
   <cfif IsNumeric(FORM.theTestValue)>
   <h3>The string <cfoutput>#DE(FORM.theTestValue)#</cfoutput> can be 
converted to a number</h3> <cfelse> <h3>The string <cfoutput>#DE(FORM.theTestValue)#</cfoutput> cannot be
converted to a number</h3> </cfif> </cfif> <form action = "isNumeric.cfm"> <p>Enter a string, and find out whether it can be evaluated to a numeric value. <p><input type = "Text" name = "TheTestValue" value = "123"> <input type = "Submit" value = "Is it a Number?" name = ""> </form>

Contents > CFML Reference > ColdFusion Functions > IsNumeric PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


Gadget318 said on Mar 15, 2004 at 2:58 AM :
Please note that using isnumeric() on its own is *not* enough to make sure you've got a numeric value. It appears that isnumeric() does a trim() before checking, so a value such as " 123 " will return TRUE, even though there are non-numeric characters in the value. Use val() to get the actual numeric value after using isnumeric().

To me this seems counter-intuitive - if a value contains *any* non-numeric characters then isnumeric() should return FALSE.

It seems that a type="numeric" in a <cfargument> tag uses the same functionality as isnumeric() and will allow " 123 " though as a numeric value.
halL said on Mar 15, 2004 at 8:05 AM :
The reason the function returns true is that such a value will be interpreted as a number, when appropriate, in a ColdFusion expression.
For example, if you run the following code:

<cfset foo=" 00123">
<cfoutput>#isnumeric(foo)#<br>
#foo + 2#
</cfoutput>

You get

YES
125

Despite the leading spaces in the foo variable.
TomGru said on Aug 30, 2004 at 8:18 AM :
<cfset test="123f">
<cfif #isnumeric(test)# is "YES">
OK
<cfelse>
NO
</cfif>

There is a bug in the isnumeric function. If you have a string with various numbers and on the end one "f" as in my demo isnumeric returns true!!!! This happens only with one f on the last digit and only with f not with other chars. isnumeric 1234ff or f23 is false but 1f , 457f or 23f is true ;-((
fizgig said on Aug 30, 2004 at 8:31 AM :
That's most likely because in Java, putting a single 'f' at the end of a number indicates that it's a float. Java treats all floating point numbers as doubles (64 bits) by default, so you need to add that 'f' to force it to treat the number as a float (32 bits.) I'm assuming since CF is so closely tied to Java that this is the case. So, "123.5f" is correctly identified as a valid number; a float with the value of 123.5.
extdw_doc said on Sep 1, 2004 at 11:14 AM :
The example returns NO in the current Blackstone version of CF, which is in development and I was not able to find a bug on this.

If you believe that your issue might result from a bug in your release of ColdFusion, however, you should submit a bug report on the Macromedia Feature Request/Bug Report Form.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funct135.htm