Checking if a number is even or odd with Coldfusion 8

Posted on Tuesday, January 12th, 2010 at 5:28 pm

I was dynamically building a two-column table this morning based on a query result set.  I created the table just fine, but realized that when my query returned an odd number of records it would not show the last row.  When I bumped up my loop by one it showed the table correctly when an odd number of records were returned, but showed an extra table row when an even number of table records were returned.

Clearly, what I needed was a way to determine if the number of records returned back was even or odd.  Once I knew this I could handle the data appropriately.  There’s no predefined methods in Coldfusion 8 that I know of that checks whether a number is even or odd.

So I wrote up this little UDF.  It takes one parameter which is the number to be checked.

I’ve pasted the function below.  Feel free to change it to fit your needs.  It’s super simple.  All it really does is divide the number to be checked in half and checks to see if the result is a decimal or not.

We know a couple things.  First, regardless of how you define and integer, we all agree it is not a decimal.  And second, that any odd number divided by two will always yield a decimal, while any even number divided by two will always yield an integer.  This difference is what we check for.

The function returns a struct. It will always return a success equal to true or false, and a message indicated the results.  The message may also indicate the problem if it fails validation.

The function does do a little bit of validation.  It checks to make sure that the number being checked is a valid integer.  So a decimal or letter will return an error in the message.  If it passes validation a third result is given which is ‘iseven’.  This will return true if even and false if odd.

Hope it helps.

<cffunction name="isEven" displayname="Is Even" description="Determines if a number is even or odd." access="public" output="false" returntype="struct">

	<!--- PASS IN NUMBER TO CHECK --->
	<cfargument name="numbertocheck" displayname="Number to Check" hint="This is the number to check if even or not." type="numeric" required="true" />

	<!--- CREATE STRUCT --->
	<cfset VARIABLES.result = structnew()>
	<cfset VARIABLES.result.success = true>
	<cfset VARIABLES.result.message = "Your number is even.">
	<cfset VARIABLES.result.iseven = true>

	<!--- MAKE SURE THAT NUMBER PASSED IN IS AN INTEGER TO BEGIN WITH --->
	<cfif isvalid("integer", ARGUMENTS.numbertocheck)>
		<!--- GET RESULT OF NUMBER DIVIDED BY TWO --->
		<cfset VARIABLES.divided = ARGUMENTS.numbertocheck / 2>

		<!--- CHECK IF DIVIDED NUMBER IS NOT AN INTEGER --->
		<cfif NOT isvalid("integer", VARIABLES.divided)>
			<!--- RESET DATA IN STRUCT TO REFLECT AN ODD NUMBER --->
			<cfset VARIABLES.result.message = "Your number is odd.">
			<cfset VARIABLES.result.iseven = false>
		</cfif>
	<cfelse>
		<cfset VARIABLES.result.success = false>
		<cfset VARIABLES.result.message = "Please pass in a valid integer to check.">
	</cfif>

	<!--- RETURN THE STRUCT --->
	<cfreturn VARIABLES.result>

</cffunction>

Tags
CategoryColdfusion

Leave a Reply

*
(Won't be published) *