DevLearningTools

🚧 This site is under active construction — new tools, guides, and pages are added every week.

MODULE 2 · LESSON 07

Numbers

Working with integers and decimals in ColdFusion — arithmetic, round(), ceiling(), floor(), randRange(), numberFormat(), min(), max(), and sqr(), in both CFScript and Tag Syntax.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

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
<cfscript>

age = 25;
price = 499.99;
temperature = -5;

</cfscript>
Tag Syntax
<cfset age = 25>
<cfset price = 499.99>
<cfset temperature = -5>

Display Numbers

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Basic Arithmetic

ColdFusion supports all common arithmetic operators.

OperatorMeaningExample
+Addition10 + 5
-Subtraction10 - 5
*Multiplication10 * 5
/Division10 / 5
%Modulus10 % 3

Arithmetic Example

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Increment and Decrement

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

Tag syntax does not support ++ or -- . Use <cfset count = count + 1> instead.

Absolute Value

Returns the positive value of a number.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Rounding Numbers

Use round() to round to the nearest whole number.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Ceiling

Always rounds upward.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Floor

Always rounds downward.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Generate Random Numbers

Use randRange().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

Every run of randRange() returns a different number in the given range — that's the point.

Format Numbers

Use numberFormat().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Format Decimal Places

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Minimum and Maximum

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Square Root

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Real-World Example: Calculate Discount

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Common Numeric Functions

FunctionPurpose
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.