« Are You Using a robots.txt File? | Main | MAX Trip Off To Slow Start »

October 29, 2004

What If You Want To Round Down?

ColdFusion's round() function rounds up. What if you want to round down? Use this:

<cffunction name="roundDown" output="no">
    <cfargument name="target" type="numeric" required="true"/>
    <cfreturn abs(round((arguments.target * -1)))/>
</cffunction>

Example:

round(1.1) = 1
roundDown(1.1) = 1

round(1.5) = 2
roundDown(1.5) = 1

round(1.6) = 2
roundDown(1.6) = 2

Addition:

Bill pointed out that the function above doesn't work with negative numbers. This one does. Thanks, Bill!

<cffunction name="roundDown" output="no">
    <cfargument name="target" type="numeric" required="true"/>
    <cfreturn (round((arguments.target * -1))) * -1/>
</cffunction>

Example:

roundDown(-1.5) = -2

Posted by cantrell at October 29, 2004 10:18 AM | References

Related Entries

Comments

Int() ?

Posted by: Devin at October 29, 2004 10:41 AM

int() doesn't round one way or the other. int() returns the closest whole number smaller than the given decimal. So int(1.9) would return 1, not 2. roundDown() works like round(), but rounds down when the argument is n.5, like this:

roundDown(1.1) = 1
roundDown(1.5) = 1
roundDown(1.6) = 2

int(), round() and roundDown() are all different.

Posted by: Christian Cantrell at October 29, 2004 10:57 AM

and wont this roundDown function totally screw you if you pass in a negative number?
If I pass in -3.7 and I want to round down (to -4) wouldn't this function retun me positive 3?

even assuming I wanted -3.7 to round down? to -3 and I passed it into this function would still return 3.

Int as Devin suggested works just like Floor and hence always rounds down doesn't it?

for instance
Int(2.1) = 2
Int(2.9) = 2
Int(-3.7) = -4
Int(-3.2) = -4

Posted by: Bill Rawlinson at October 29, 2004 10:58 AM

i didn't see your response to Devin when I was posting; so i see your point about the Int not "rounding" based on the .5 increment.

However, negatives in roundDown still cause a problem don't they?

ie roundDown(-3.7) reuturns 4
and roundDown(-3.2) returns 3

Posted by: Bill at October 29, 2004 11:01 AM

You're right, Bill. See the addition to the main post. Thanks!

Posted by: Christian Cantrell at October 29, 2004 11:12 AM

my last post, i promise..
so basically all this does is handle values at exactly half decimal point increments differently ie(.5)

ie:

round(2.1) = 2
round(2.8) = 3
round(2.5) = 3
round(-3.7) = -4
round(-3.2) = -3
round(-3.5) = -3

AND

roundDown(2.1) = 2
roundDown(2.8) = 3
roundDown(2.5) = 2
roundDown(-3.7) = -4
roundDown(-3.2) = -3
roundDown(-3.5) = -4

Posted by: Bill Rawlinson at October 29, 2004 11:19 AM

Yeah I noticed that the abs would cause unwanted results. The fix is simple:




you simple multiply by -1 again at the end to switch the sign to the original sign.

Posted by: Eric Moritz at October 29, 2004 11:31 AM

A couple of people have pinged me about this post, and it seems I've created more confusion than clarity. Bill, you're right -- all this function does is round half decimal points down rather than up as round() does. I ran into a circumstance the other day (when putting together the MXNA Mobile site) where I was calculating a value that I needed to round down rather than up (it had something to do with the "stars" function). This is certainly not a normal circumstance as you typically want to round up (0-4 down, 5-9 up), but sometimes you need to work against rationality to make something work -- or at least I do.

Sorry for the confusion!

Christian

Posted by: Christian Cantrell at October 29, 2004 11:32 AM

oops, didn't know the markup would disappear:
here's the formula:

round(arguments.target * -1) *-1

Posted by: Eric Moritz at October 29, 2004 11:33 AM

Hi!!! this tess clune. I don't know if this is the right eric moritz, but if it is please email me!!!!!!!!!wonering how you are doing!!!!!!

Posted by: tess at January 21, 2005 05:35 PM

Eric, my email address is Siren_may@yahoo.com. Wondering what your up to!!!!!!!!!!!!!!!!!!!!!!

Posted by: tess at February 3, 2005 06:45 PM