[thelist] IsNumeric() vs. Val()

.jeff jeff at members.evolt.org
Wed Feb 27 01:24:01 CST 2002


erik,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Erik Mattheis
>
> Is there any difference between CF's IsNumeric() and
> Val() other than you can get a number 2 from 2xxx if
> you use Val()? Do I understand Val()?
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

IsNumeric() returns a boolean if the string that's passed to it is a valid
number.  Val(), on the other hand, attempts to return a number from a string
that's passed to it.  if it can't convert, it returns 0 (zero).  in and of
itself, this can be used as a boolean check as well since 0 is false and
anything else is true.  a word of caution with the use of Val() though, it
can't handle integers of more than 10 digits long.  the following:

<cfoutput>
  <cfset string = "">
  <cfloop from="1" to="20" index="i">
    <cfset string = string & i>
    #string# - #Val(string)#<br>
  </cfloop>
</cfoutput>

results in:

1 - 1
12 - 12
123 - 123
1234 - 1234
12345 - 12345
123456 - 123456
1234567 - 1234567
12345678 - 12345678
123456789 - 123456789
12345678910 - 12345678910
1234567891011 - 1.23456789101E+012
123456789101112 - 1.23456789101E+014
12345678910111213 - 1.23456789101E+016
1234567891011121314 - 1.23456789101E+018
123456789101112131415 - 1.23456789101E+020
12345678910111213141516 - 1.23456789101E+022
1234567891011121314151617 - 1.23456789101E+024
123456789101112131415161718 - 1.23456789101E+026
12345678910111213141516171819 - 1.23456789101E+028
1234567891011121314151617181920 - 1.23456789101E+030

this doesn't become a problem unless you have database records with pk's
more than 10 digits in length.  not a problem for many people, but something
i've run up against.

good luck,

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/




More information about the thelist mailing list