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

<cffunction name="roundToX" returntype="numeric">
   <cfargument name="num" type="numeric" required="yes">
   <cfargument name="roundPoint" type="numeric" required="yes">
   
   <cfset numRounded = round(num/roundPoint)*roundPoint>
   
   <cfreturn numRounded>
</cffunction>
So roundToX(269000,25000) = 275000

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. :)

Comments
round(num/roundPoint)*roundPoint will work just dandy, and save the extra variable (which you didn't var-scope, I might add ; ) and conditional.
# Posted By Barney | 4/5/06 5:25 PM
Doh! Barney, weren't you the guy that suggested using ceiling? Where was this brilliant idea yesterday on cftalk? ;)
# Posted By yacoubean | 4/6/06 9:03 AM
I certainly was. But on CF-Talk, he wanted upper and lower bounds, not rounding. He did use "round" in his request, but the context illustrated that it's not what he really wanted. If you want bounds, you have to use ceiling and int/fix explicitly to ensure you go the right way (up for upper, down for lower), but if you want actual rounding, the round function is sufficient.

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).
# Posted By Barney | 4/6/06 9:44 AM
Oh yeah, I got sidetracked away from the original issue. I was thinking it would be cool to have a generic round function, which /could/ be used by his app, but wouldn't get the right results (as you stated).
# Posted By yacoubean | 4/6/06 12:37 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner