View comments | RSS feed

Val

Description

Converts numeric characters that occur at the beginning of a string to a number.

Return value

A number. If conversion fails, returns zero.

Category

Conversion functions, String functions

Syntax

Val(string)

See also

IsNumeric

Parameters

Parameter Description
string
A string or a variable that contains one

Usage

This function works as follows:

Example

<h3>Val Example</h3>
<cfif IsDefined("FORM.theTestValue")>
  <cfif Val(FORM.theTestValue) is not 0>
    <h3>The string <cfoutput>#DE(FORM.theTestValue)#</cfoutput> 
can be converted to a number:
    <cfoutput>#Val(FORM.theTestValue)#</cfoutput></h3>
  <cfelse>
    <h3>The beginning of the string <cfoutput>#DE(FORM.theTestValue)#
     </cfoutput> cannot be converted to a number</h3>
  </cfif>
</cfif>
<form action = "val.cfm">
<p>Enter a string, and determine whether its beginning can be evaluated 
to a numeric value.
<p>
<input type = "Text" 
  name = "TheTestValue" 
  value = "123Boy">
<input type = "Submit" 
  value = "Is the beginning numeric?" 
  name = "">
</form>

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

Version 6

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

Comments


No screen name said on Aug 26, 2003 at 7:20 AM :
May seem obvious, but val done on an empty string returns zero.
beadingo said on Aug 11, 2004 at 2:54 AM :
If you need to get the number of the end of a string
e.g. HAIKU-2004 you can do this, reverse the string do val and then reverse it again.

reverse(val(reverse(textstring))
ivanmca said on Sep 23, 2004 at 10:31 AM :
That trick will reverse() will only work if the number does not end in zero, e.g. HAIKU-2004 -> 2004 but HAIKU-200 -> 2

 

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

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt2114.htm