View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > Round PreviousNext

Round

Rounds a number to the closest integer.

An integer.

Mathematical functions

Round(number)

Ceiling, Fix, Int

Parameter

Description

number

Number to round

<h3>Round Example</h3>
<p>This function rounds a number to the closest integer.
<ul>
   <li>Round(7.49) : <cfoutput>#Round(7.49)#</cfoutput>
   <li>Round(7.5) : <cfoutput>#Round(7.5)#</cfoutput>
   <li>Round(-10.775) : <cfoutput>#Round(-10.775)#</cfoutput>
   <li>Round(1.2345*100)/100 : <cfoutput>#Round(1.2345*100)/100#</cfoutput>
</ul>

Contents > CFML Reference > ColdFusion Functions > Round PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


CFGumby said on Aug 18, 2003 at 4:29 PM :
What does/should Round() do to a value of -35.5, for example and should it be different than CF4.5?
halL said on Aug 19, 2003 at 7:09 AM :
ColdFusion rounds up.

Round("-35.5") = -35
Round("35.5") = 36

This was generated by the following code:

<cfoutput>
Round("-35.5") = #Round("-35.5")#<br>
Round("35.5") = #Round("35.5")#
</cfoutput>
CFGumby said on Aug 20, 2003 at 10:13 AM :
I actually agree but why is it different than CF 4.5? Seems it should be consistant.
NeilR said on Oct 20, 2004 at 2:45 AM :
i agree with CFgrumpy consistency is good but if you need a fix you could use Int() as that will always round down
jonescynw said on Jan 12, 2005 at 12:26 PM :
I need to round a 2 -5 decimal place number to the nearest 2 decimal places (.45678 should round to .46, .44456 should round to .44). Do I use round? How do I specify the number of decimals?
t.j.hill said on Jan 14, 2005 at 9:45 AM :
jonescynw , use NumberFormat in order to round to some number of decimal places.
xigarete said on Jan 20, 2005 at 9:23 AM :
Numberformat only displays the number, if you need to actually round it use:

<cfset variables.newnumber = Round(variables.oldnumber * 100) / 100 >

This doesn't address the question of what to do with .5s - if you want to reverse the direction of rounding .5s then subtract a very small number - 0.000 000 1, for instance, just before you round. This should give the same right result except for values very close to .5, the desired result for exactly .5, and the wrong result only for values like 0.500 000 09, where your "small number" wasn't small enough.
surenr said on Feb 23, 2005 at 9:01 AM :
Suppose you have arrat of decimal numbers,how will it be possible to convert into int.I tred the below code and it does'nt look like it is working:

<cfloop index="loopcount" from="1" to="40">
temp[loop]=round(decimalarray[loop])
</cfloop>

the temp array should have the int values and the decimalarray should have the decimal values.
No screen name said on Mar 17, 2006 at 1:08 PM :
Try this. It will work.
function roundDecimal(number,unitprecision) {
var multiplier = 1;
for(i = 1; i lte unitprecision; i = i + 1)
{
multiplier = multiplier & "0";
}
number = multiplier * number;
number = round(number);
number = number/multiplier;
return(number);
}
babas said on Apr 11, 2006 at 7:48 AM :
or this...
<cffunction name="roundDecimal" returntype="numeric">
<cfargument name="num" type="numeric" required="yes">
<cfargument name="decimal" type="numeric" default="2" required="no">
<cfreturn round(num*10^decimal)/10^decimal>
</cffunction>

<cfoutput>
#roundDecimal(10.256,0)#<br>
#roundDecimal(10.256,1)#<br>
#roundDecimal(10.256,2)#<br>
#roundDecimal(10.256,3)#<br>
#roundDecimal(10.256,4)#<br>
</cfoutput>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functa76.htm