Round to the nearest x in ColdFusion
On CFTalk today someone wanted to round to the nearest 25000 (it was a real estate app). He got some good suggestions, but I tried them and none of them worked 100% by themselves. So I combined them all and made a function to do this. It takes two parameters, the number you want to round, and the round factor (25000 in the above example).
<cfargument name="num" type="numeric" required="yes">
<cfargument name="roundPoint" type="numeric" required="yes">
<cfset numRounded = round(num/roundPoint)*roundPoint>
<cfreturn numRounded>
</cffunction>
Update: Barney commented and basically invalidated most of what I was doing, and reduced it all to one line. So I've changed the above function to simplify it. :)
Jake Munson
34 Yrs old
Perhaps an example would be in order. If I have a range of prices from $74,000 to $101,000, rounding those would give a range of $75,000 to $100,000, which doesn't include either price (!). Instead, I want to use int/fix on the smallest (taking $74,000 to $50,000) and ceiling on the largest (taking $101,000 to $125,000).