## 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```

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`

### 11 Responses to What If You Want To Round Down?

1. Devin says:

Int() ?

2. 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) = 1roundDown(1.5) = 1roundDown(1.6) = 2int(), round() and roundDown() are all different.

3. 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 instanceInt(2.1) = 2Int(2.9) = 2Int(-3.7) = -4Int(-3.2) = -4

4. Bill says:

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 4and roundDown(-3.2) returns 3

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

6. 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) = 2round(2.8) = 3round(2.5) = 3round(-3.7) = -4round(-3.2) = -3round(-3.5) = -3ANDroundDown(2.1) = 2roundDown(2.8) = 3roundDown(2.5) = 2roundDown(-3.7) = -4roundDown(-3.2) = -3roundDown(-3.5) = -4

7. Eric Moritz says:

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.

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

9. Eric Moritz says:

oops, didn’t know the markup would disappear:here’s the formula:round(arguments.target * -1) *-1

10. tess says:

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!!!!!!

11. tess says: