ColdFusion is a loosely typed language. A variable doesn't have a fixed type — the same variable can hold a string today and a number tomorrow. This flexibility is convenient, but it also means values often need to move between types: form fields arrive as strings, database columns come back as specific types, and calculations expect numbers.
ColdFusion converts many values automatically, but relying on automatic conversion for everything can hide bugs. Knowing the explicit conversion and validation functions gives you control over exactly how and when a value changes type.
By the end of this lesson, you'll understand ColdFusion's automatic type conversion, know how to explicitly convert between strings, numbers, and dates, and know how to validate a value's type before using it.
Learning Objectives
After completing this lesson, you'll be able to:
- Understand automatic (implicit) type conversion in ColdFusion.
- Convert a string to a number.
- Convert a number to a string.
- Convert a number to an integer.
- Parse strings into numbers and dates strictly.
- Validate whether a value is numeric, boolean, or a simple value.
- Avoid common type conversion mistakes.
What Is Type Conversion?
Type conversion is changing a value from one data type to another — for example, turning the string "25" into the number 25, or the number 25 into the string "25".
ColdFusion performs some conversions automatically (implicit conversion). For everything else, it provides functions to convert or validate a value explicitly.
Automatic (Implicit) Type Conversion
ColdFusion automatically converts values to satisfy an expression. For example, adding a numeric string to a number works without any conversion function.
Implicit conversion is convenient, but it can hide bugs. "5" + 3 works, but "five" + 3 throws an error because "five" cannot convert to a number. Always validate values that come from user input.
Conversion Rules to Know
A few conversions come up often enough to memorize:
| Value | As Number | As String |
|---|---|---|
| "Yes" | 1 | "Yes" |
| "No" | 0 | "No" |
| "eight" | Error | "eight" |
Adobe's full conversion reference lists every Boolean/number/date-time/string combination — see Further Reading below for the complete table.
Converting a String to a Number
Use val(). It reads leading numeric characters and returns 0 if there aren't any — it never throws an error.
Strict Number Parsing
Use parseNumber() when you want to reject invalid input instead of silently getting 0. Unlike val(), it throws an error if the string isn't a valid number.
parseNumber("123abc") throws an error. Use isNumeric() first if you need to check a value before parsing it.
Converting a Number to an Integer
Use int() or fix(). Both remove the decimal portion, but they handle negative numbers differently.
int() rounds toward negative infinity (like floor()), so int(-4.9) is -5. fix() truncates toward zero, so fix(-4.9) is -4. For rounding to the nearest whole number, use round() — covered in the Numbers lesson.
Converting a Value to a String
Use toString().
Checking Whether a Value Is Numeric
Use isNumeric() to validate a value before converting it — especially useful for form input.
Checking Whether a Value Is a Boolean
Use isBoolean(). ColdFusion treats strings like "yes", "no", "true", and "false" as valid boolean values.
Strict Date Parsing
Use parseDateTime() to convert a string into a real date object. It's stricter and more predictable than relying on ColdFusion to guess a date implicitly — see the Date & Time lesson for isDate() and the other date functions.
Checking for a Simple Value
Use isSimpleValue() to check whether a value is a string, number, boolean, or date — as opposed to a complex type like an array, struct, or query.
Real-World Example: Validating Form Input
Form fields always arrive as strings. Validate before converting so a bad input can't crash a calculation.
Common Beginner Mistakes
Trusting Implicit Conversion for User Input
"quantity" + 5 works if quantity is "3", but throws an error if quantity is "three". Validate with isNumeric() before relying on automatic conversion for values that come from users.
Assuming int() Rounds Like round()
int(4.9) is 4, not 5 — int() always rounds toward negative infinity, it doesn't round to the nearest whole number. Use round() when you need standard rounding.
Using val() When Invalid Input Should Be an Error
val("abc") silently returns 0 instead of failing, which can hide bad data. Use parseNumber() or isNumeric() when invalid input needs to be caught, not defaulted to zero.
Best Practices
- Validate form and user input with isNumeric() or isDate() before converting it.
- Use val() when a safe default of 0 is acceptable; use parseNumber() when invalid input should raise an error.
- Remember int() truncates toward negative infinity, not the nearest whole number — use round() for that.
- Use parseDateTime() instead of relying on ColdFusion to guess ambiguous date strings.
- Prefer explicit conversion functions over implicit conversion for values you don't fully control.
Frequently Used Type Conversion Functions
| Function | Purpose |
|---|---|
| val() | Convert leading numeric characters to a number; returns 0 otherwise |
| parseNumber() | Strictly parse a string into a number |
| int() | Integer part of a number, rounded toward negative infinity |
| fix() | Integer part of a number, truncated toward zero |
| toString() | Convert a value to a string |
| isNumeric() | Check whether a value is numeric |
| isBoolean() | Check whether a value is a valid boolean |
| isDate() | Check whether a value is a valid date |
| parseDateTime() | Strictly parse a string into a date object |
| isSimpleValue() | Check whether a value is a string, number, boolean, or date |
Interview Questions
What is the difference between val() and parseNumber()?
val() reads leading numeric characters and returns 0 if there aren't any, without throwing an error. parseNumber() strictly parses the string and throws an error if it isn't a valid number.
What is the difference between int() and fix()?
int() rounds toward negative infinity, so int(-4.9) is -5. fix() truncates toward zero, so fix(-4.9) is -4.
How do you check whether a string is a valid number before converting it?
Use isNumeric().
Which function converts a value to a string?
toString()
Why is relying only on implicit type conversion risky for user input?
ColdFusion throws an error if a value can't convert to the type an operation needs, so unvalidated user input (like a non-numeric string used in arithmetic) can crash the request.
Summary
In this lesson, you learned how ColdFusion converts values between types, both automatically and explicitly. You converted strings to numbers with val() and parseNumber(), converted numbers to integers with int() and fix(), converted values to strings with toString(), validated values with isNumeric(), isBoolean(), and isSimpleValue(), and parsed dates strictly with parseDateTime(). These functions help you control exactly how and when a value changes type, instead of relying entirely on ColdFusion's automatic conversion.
What's Next?
The next lesson starts the Decision Making module with the if statement — how to run code conditionally based on whether an expression is true or false, in both CFScript and Tag Syntax.