Rounding numbers in ColdFusion

Readability

Rounding numbers in ColdFusion

The fol­low­ing exam­ple shows how you can round num­bers using the round(), fix, int() and ceiling() methods.


pi(): #pi()#
round(): #round(pi())#
fix(): #fix(pi())#
int(): #int(pi())#
ceiling(): #ceiling(pi())#

The pre­ced­ing code pro­duces the fol­low­ing output:

pi(): 3.14159265359
round(): 3
fix(): 3
int(): 3
ceil­ing(): 4

#round()#
Rounds a num­ber to the clos­est inte­ger that is larger than the input parameter.
#fix()#
Con­verts a real num­ber to an integer.
#int()#
Cal­cu­lates the clos­est inte­ger that is smaller than num­ber. For exam­ple, it returns 3 for Int(3.3) and for Int(3.7); it returns –4 for Int(-3.3) and for Int(-3.7).
#ceil­ing()#
Deter­mines the clos­est inte­ger that is greater than a spec­i­fied number.

The following example shows how you can round numbers using the round(), fix, int() and ceiling() methods.

<cfoutput>
pi(): #pi()#<br/>
round(): #round(pi())#<br/>
fix(): #fix(pi())#<br/>
int(): #int(pi())#<br/>
ceiling(): #ceiling(pi())#<br/>
</cfoutput>

The preceding code produces the following output:

pi(): 3.14159265359
round(): 3
fix(): 3
int(): 3
ceiling(): 4

#round()#
Rounds a number to the closest integer that is larger than the input parameter.
#fix()#
Converts a real number to an integer.
#int()#
Calculates the closest integer that is smaller than number. For example, it returns 3 for Int(3.3) and for Int(3.7); it returns -4 for Int(-3.3) and for Int(-3.7).
#ceiling()#
Determines the closest integer that is greater than a specified number.

One thought on “Rounding numbers in ColdFusion”

  1. The ColdFusion documentation for the Ceiling function is wrong. It actually returns the closest integer greater than or _equal_ to the argument. Ceiling(3) is 3, for example.

Leave a Reply

Your email address will not be published.