Numbers are used everywhere in web applications. Whether you're calculating prices, processing payments, generating invoices, computing discounts, tracking inventory, or displaying statistics, you'll work with numeric values.
ColdFusion provides powerful built-in functions for performing mathematical calculations, rounding values, formatting numbers, and generating random numbers.
By the end of this lesson, you'll know how to work with integers and decimal numbers, perform arithmetic operations, use common mathematical functions, and format numbers for display.
Learning Objectives
After completing this lesson, you'll be able to:
- Understand numeric data types.
- Create integer and decimal numbers.
- Perform mathematical calculations.
- Round numbers.
- Format numbers.
- Generate random numbers.
- Use common ColdFusion numeric functions.
What Is a Number?
A number represents a numeric value that can be used in calculations — age, price, salary, quantity, percentage, temperature, and so on. Examples: 10, 250, 99.99, -15.
Unlike strings, numbers can be added, subtracted, multiplied, and divided.
Creating Numbers
<cfscript> age = 25; price = 499.99; temperature = -5; </cfscript>
<cfset age = 25> <cfset price = 499.99> <cfset temperature = -5>
Display Numbers
Basic Arithmetic
ColdFusion supports all common arithmetic operators.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| - | Subtraction | 10 - 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| % | Modulus | 10 % 3 |
Arithmetic Example
Increment and Decrement
Tag syntax does not support ++ or -- . Use <cfset count = count + 1> instead.
Absolute Value
Returns the positive value of a number.
Rounding Numbers
Use round() to round to the nearest whole number.
Ceiling
Always rounds upward.
Floor
Always rounds downward.
Generate Random Numbers
Use randRange().
Every run of randRange() returns a different number in the given range — that's the point.
Format Numbers
Use numberFormat().
Format Decimal Places
Minimum and Maximum
Square Root
Real-World Example: Calculate Discount
Common Numeric Functions
| Function | Purpose |
|---|---|
| abs() | Absolute value |
| round() | Round number |
| ceiling() | Round up |
| floor() | Round down |
| rand() | Random decimal |
| randRange() | Random integer |
| numberFormat() | Format numbers |
| min() | Smaller value |
| max() | Larger value |
| sqr() | Square root |
| incrementValue() | Increment by 1 |
| decrementValue() | Decrement by 1 |
Common Beginner Mistakes
Storing Numbers as Strings
price = "500"; stores 500 as text, which can cause unexpected behavior in calculations or comparisons. Write price = 500; to store it as a real number.
Dividing by Zero
10 / 0 causes an error in ColdFusion — always validate a divisor isn't zero before dividing by it.
Using Integer When Decimal Is Needed
price = 99 loses precision for values like 99.99. Write price = 99.99 whenever decimal precision matters, such as currency.
Best Practices
- Store numeric values as numbers, not strings.
- Use numberFormat() for displaying currency and reports.
- Validate user input before calculations.
- Avoid division by zero.
- Use descriptive variable names such as totalPrice, taxAmount, and discountRate.
Interview Questions
What is a numeric data type?
A numeric data type stores integer or decimal values used in calculations.
Which function rounds a number?
round()
Which function formats a number?
numberFormat()
Which function generates a random integer?
randRange()
What is the difference between ceiling() and floor()?
ceiling() always rounds upward, while floor() always rounds downward.
Summary
In this lesson, you learned how to work with numbers in ColdFusion using both CFScript and Tag Syntax. You performed arithmetic operations, rounded values, generated random numbers, formatted numeric output, and explored commonly used numeric functions. These skills are fundamental for calculations in business applications such as billing, payroll, reporting, and e-commerce.
What's Next?
The next lesson covers Date & Time — how to create dates, display the current date and time, format dates, add or subtract days, compare dates, calculate date differences, and use ColdFusion's built-in date and time functions in both CFScript and Tag Syntax.