String and Integer data typing catch using compare()

Posted on Monday, November 1st, 2010 at 8:42 pm

I just previously wrote a post on the loose data typing in Coldfusion and how, although I could see the potential for problems, I have never actually had any issues that I’ve known about due to not having strict data typing. As fate would have it, of course, I ran into an issue today related to data typing using the Coldfusion compare() function. The issue is when two numbers get accidentally typed as a string behind the scenes and it doesn’t become known until you realize that there are inaccuracies in the comparison. It’s very important to note this because it almost made it past me.

The issue occurs when you compare the strings of integers and they get compared not as an integer, but as strings. The difference is that integers get compared as whole numbers, while strings get compared on a character by character basis. So for example comparing the string “3″ and the string “100″ will indicate that “100″ is less than “3″. The reason for this is because when looking at the first character “1″ is indeed less than “3″. Comparing the two as integers will of course indicate that 3 is less than 100.

So speaking more specific, the next thing to note is that the compare() function compares strings. This essentially makes it useless comparing integers. Maybe I’m wrong, but a quick search of the docs didn’t return any comparison function for integers. So the only way I know to handle it is to use an if/else statement. Let’s take a look at what we’re dealing with using the compare() function to compare either integers typed as strings or integers typed as integers, because the compare() function will implicitly type the data as strings.

String Comparison (cfscript)

public void function typeDataCatch()
	displayname="Type Data Catch" hint="Shows a data type catch using compare function." output="false"
{
	var data1 = "2";
	var data2 = "100";

	var dataComparison = compare(data1,data2);  //returns 1 indicating that 100 is less than 2
}

String Comparison (tags)

<cffunction name="typeDataCatch" displayname="Type Data Catch" hint="Shows a data type catch using compare function." output="false" access="public" returntype="void">

	<cfset var data1 = "2" />
	<cfset var data2 = "100" />

	<cfset var dataComparison = compare(data1, data2) />  <!--- will return 1 indicating that 100 is less than 2 --->

</cffunction>

So let’s take a look at performing the same operation on integers.

Integer Comparison (cfscript)

public void function typeDataCatch()
	displayname="Type Data Catch" hint="Shows a data type catch using compare function." output="false"
{
	var data1 = 2;
	var data2 = 100;

	compare(data1,data2);  //will also return 1 indicating that 100 is less than 2
}

Integer Comparison (tags)

<cffunction name="typeDataCatch" displayname="Type Data Catch" hint="Shows a data type catch using compare function." output="false" access="public" returntype="void">

	<cfset var data1 = 2 />
	<cfset var data2 = 100 />

	<cfset var dataComparison = compare(data1, data2) /> <!--- will also return 1 indicating that 100 is less than 2 --->

</cffunction>

So, what if you run into a situation where you have two strings that need to be compared as integers to get accurate results. As I discussed in my previous post, there is, to my knowledge, no type conversion function for typing a string as an integer. However, I pointed out that Coldfusion does perform implicit conversion when arithmetic is performed on two strings with integers. As such, you can quickly convert the strings to integers by simply adding 0 to them. Let’s look at our first example using this method of type conversion and an if/else statement to get the results we’re after.

Typing string as an integer (cfscript)

public void function typeDataCatch()
	displayname="Type Data Catch" hint="Shows a data type catch using compare function." output="false"
{
	var data1 = "2";
	var data2 = "100";
	var dataComparison = 0;

	data1 = data1 + 0;
	data2 = data2 + 0;

	if(data1 > data2)
	{
		dataComparison = 1;
	}
	else if(data1 == data2)
	{
		dataComparison = 0;
	}
	else
	{
		dataComparison = -1;
	}
}

Typing string as an integer (tags)

<cffunction name="typeDataCatch" displayname="Type Data Catch" hint="Shows a data type catch using compare function." output="false" access="public" returntype="void">

	<cfset var data1 = "2" />
	<cfset var data2 = "100" />
	<cfset var dataComparison = 0 />

	<cfset data1 = data1 + 0 />
	<cfset data2 = data2 + 0 />

	<cfif data1 gt data2>
		<cfset dataComparison = 1 />
	<cfelseif data1 eq data2>
		<cfset dataComparison = 0 />
	<cfelse>
		<cfset dataComparison = -1 />
	</cfif>

</cffunction>

So something to pay attention to when comparing. Remember that a string comparison will return different results from that of an integer comparison.

Tags
CategoryColdfusion

Leave a Reply

*
(Won't be published) *