val
Description
Converts numeric characters that occur at the beginning of a string to a number.
Categories
Related
Syntax
Val(string)
Attributes
| Attribute | Description | Required | Default |
|---|---|---|---|
| string | A string or a variable that contains one |
Returns
A number.
If conversion fails, returns zero.
Usage
This function works as follows:
If TestValue = "234A56?7'", Val(TestValue) returns 234.
If TestValue = "234'5678'9?'", Val(TestValue) returns 234.
If TestValue = "BG234", Val(TestValue) returns the value 0, (not an error).
If TestValue = "0", Val(TestValue) returns the value 0, (not an error).
If TestValue = "234A56?7'", Val(TestValue) returns 234.
If TestValue = "234'5678'9?'", Val(TestValue) returns 234.
If TestValue = "BG234", Val(TestValue) returns the value 0, (not an error).
If TestValue = "0", Val(TestValue) returns the value 0, (not an error).
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">
Enter a string, and determine whether its beginning can be evaluated
to a numeric value.
<input type = "Text"
name = "TheTestValue"
value = "123Boy">
<input type = "Submit"
value = "Is the beginning numeric?"
name = "">
</form>