Custom rounding in ColdFusion

Yesterday a friend asked an interesting question. Normal rounding is based on the rule we are familiar with. Anything decimal point that is 5 and higher will round up to the next number. So 11.5 becomes 12. My friend though had a client who wanted to change the rules. Instead of rounding on 5, they wanted to round on 4. So 11.4 would become 12. 11.39 would round down to 11. Sounded weird to me, but maybe that’s what the Wall Street folks have been doing lately. I whipped up a quick function that treated numbers like strings:

function myRound2(x) { if(listLen(x,".") is 1) return x; if(left(listLast(x, "."),1) >= 4) return incrementValue(listFirst(x,".")); return listFirst(x,"."); }

This worked fine until he tested with a negative number. He came back with a nicer solution (makes me wonder if he will ever ask me again for help!) that worked in all cases:

function myRound(x) { if (abs(x - int(x)) >= .40) return ceiling(x); else return int(x); }

His solution correctly handled the negative numbers. You could even make it a bit more abstract:

function myRound(x,y) { if (abs(x - int(x)) >= (y/10)) return ceiling(x); else return int(x); }

This will let me pass any value for the rounding limit.

Anyway, I’ll use the above as a good springboard to remind folks about unit testing. When he found the issue with my code and negative numbers, it would have been the perfect place to write a unit test to ensure that never became a problem again.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments