Did You Know That "000" Equals "00A" In ColdFusion?

Posted March 6, 2007 at 6:14 PM

Tags: ColdFusion

Dan Roberts just pointed out this very strange bug to me; apparently in ColdFusion the strings "0" (zero) and "A" are not equal unless they are preceded by zeros, in which case they are considered equal. I am sure this is a known bug, but I had never seen it, so I thought I would throw it out there.

To test this, I ran a bunch of comparison tests to see what worked and what failed:

 Launch code in new window » Download code as text file »

  • <!--- Set the two strings for comparison. --->
  • <cfset strA = "00A" />
  • <cfset strB = "000" />
  •  
  •  
  • <p>
  • <cfif (strA EQ strB)>
  •  
  • #strA# EQ #strB#
  •  
  • <cfelse>
  •  
  • #strA# NEQ #strB#
  •  
  • </cfif>
  • </p>
  •  
  • <p>
  • <cfif (ToString( strA ) EQ ToString( strB ))>
  •  
  • #strA# EQ #strB# (Using CF ToString())
  •  
  • <cfelse>
  •  
  • #strA# NEQ #strB# (Using CF ToString())
  •  
  • </cfif>
  • </p>
  •  
  • <p>
  • <cfif (strA.ToString() EQ strB.ToString())>
  •  
  • #strA# EQ #strB# (Using Java ToString())
  •  
  • <cfelse>
  •  
  • #strA# NEQ #strB# (Using Java ToString())
  •  
  • </cfif>
  • </p>
  •  
  • <p>
  • <cfif strA.Equals( strB )>
  •  
  • #strA# EQ #strB# (using Java Equals())
  •  
  • <cfelse>
  •  
  • #strA# NEQ #strB# (using Java Equals())
  •  
  • </cfif>
  • </p>
  •  
  • <p>
  • <cfif NOT Compare( strA, strB )>
  •  
  • #strA# EQ #strB# (using Compare)
  •  
  • <cfelse>
  •  
  • #strA# NEQ #strB# (using Compare)
  •  
  • </cfif>
  • </p>
  •  
  • <p>
  • <cfif NOT strA.CompareTo( strB )>
  •  
  • #strA# EQ #strB# (using CompareTo())
  •  
  • <cfelse>
  •  
  • #strA# NEQ #strB# (using CompareTo())
  •  
  • </cfif>
  • </p>

None of these should come out as being equal, but running the above code gives us the following output:

00A EQ 000

00A EQ 000 (Using CF ToString())

00A EQ 000 (Using Java ToString())

00A NEQ 000 (using Java Equals())

00A NEQ 000 (using Compare)

00A NEQ 000 (using CompareTo())

As you can see, the standard ColdFusion "EQ" operator works on all string values (no matter how that string value was generated). But, all of the more "Java-oriented" and character-wise approaches truthfully tell the two strings apart. This doesn't seem to happen with anything but "0" and "A". Very strange.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Mar 6, 2007 at 6:39 PM // reply »
18 Comments

I would let the CF team know.


Mar 6, 2007 at 6:49 PM // reply »
27 Comments

I submitted a bug report to Adobe not much earlier today.

Thanks for testing all of the equality functions/operators. I had to add some extra code to deal with this but will probably just switch it out with Compare() tomorrow.


Mar 6, 2007 at 6:53 PM // reply »
6,516 Comments

@Dan,

I would be curious to know how you even found this?!? Also as far as moving to the Compare() method... you probably want to move to CompareNoCase() as it is case-insensitive and the EQ operator is also case-insensitive.


Mar 6, 2007 at 7:06 PM // reply »
27 Comments

The company I work for manages a database of federal and states legislators. For some odd reason my boss decide way back when that the district field should have leading zeros, which he says makes it easier to eyeball problems. For example a district 1 is 001, a district A is 00A and officials without a district have 000. I was checking for district fields of 000 and of course ran into problems when records with 00A matched as well.

This came up a couple years ago but I just threw in a quick IsNumeric check which 00A fails on. That worked so I commented about the problem and moved on. Saw the comment today and thought it was about time I should bring it to their attention.


Mar 7, 2007 at 2:48 AM // reply »
24 Comments

Hi,

just did a small test and found that the IS operator behaves just like EQ.

Chris


Mar 7, 2007 at 6:48 AM // reply »
2 Comments

The following link has some interesting information on the subject.

http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00000909.htm

Regarding the above problem it says:

"Short strings, such as 1a and 2P, can produce unexpected results. ColdFusion can interpret a single "a" as AM and a single "P" as PM. This can cause ColdFusion to interpret strings as date-time values in cases where this was not intended."

So coldfusion is comparing 0:00 AM to 0:00 AM.


Mar 7, 2007 at 7:53 AM // reply »
6,516 Comments

@Gary,

Nicely done! I guess this is ColdFusion's attempt to make things super easy for people to use (and at the same time, sacrificing a tiny bit of predictability). I certainly do love how easy it is to compare date/times in CF.


Mar 7, 2007 at 9:30 AM // reply »
27 Comments

Thanks Gary. That gives me some idea of what's probably going on.

Oddly though I can only get it to happen with EQ using 0 and 0A. Doesn't happen with 1 and 1A for example or 13 and 1P. GT and LT only compare like this if you include letters on both strings. Anyways, something to keep in mind.


Mar 7, 2007 at 9:41 AM // reply »
2 Comments

1 and 1a dont work because coldfusion turns the time into a fraction.

So for example the values

<cfset strA = "0.25" />
<cfset strB = "6A" />

would work. As strB is turned into 6:00 AM which is a quarter of a day (6/24 = 0.25).


Mar 7, 2007 at 9:43 AM // reply »
24 Comments

@Daniel:

Try "1p" EQ "13:"

Note the colon which, I guess, makes CF believe the string actually is the hour part of a time value.

Best,

Chris


Mar 7, 2007 at 12:46 PM // reply »
30 Comments

I recently had a somewhat similar problem with some floating point values in CF. I was comparing values from records in two different sets of SQL Server query results, values that when cfdumped to the screen were exactly the same, yet the EQ comparison always evaluated as false. I tried doing a javacast() of both values to double and comparing them, but the EQ still came back false. Again, cfdumping the returned values from javacast() seemed to confirm they were the exact same values. So I tried converting the values using DecimalFormat(). The EQ comparison correctly evaluated as true.


Mar 7, 2007 at 2:36 PM // reply »
38 Comments

Thought you guys might find it interesting to know the results of this code on Scorpio and BlueDragon (I have access to both). Scorpio is identical to CF 7, so they haven't 'fixed' this (yet). However, BlueDragon does it all correctly:
http://techfeed.net/000-00A.cfm


Mar 7, 2007 at 2:39 PM // reply »
6,516 Comments

Go Blue Dragon! I can't wait to get my hands on Scorpio!


Mar 7, 2007 at 3:15 PM // reply »
38 Comments

We are lucky enough to have an ex-Adobe employee on our team, and he was able to get us a copy of Scorpio Beta. I wish I could share some of the new stuff they've got in there, but maybe it suffices to say that I'm very excited about the additions. And I'm not talking about all the boring Adobe product integration we've already heard about.


Mar 7, 2007 at 3:17 PM // reply »
6,516 Comments

I want to see if I can get a Beta. Apparently there is some program for it... time to do some snooping.


Mar 7, 2007 at 3:26 PM // reply »
24 Comments

@Ben,

AFAIK the Scorpio Beta is public. Ben Forta posted a link to the Adobe Prerelease Program in his blog: http://www.forta.com/blog/index.cfm/2007/1/22/Anyone-Want-To-Beta-Test-Scorpio

Best,

Chris


Mar 7, 2007 at 3:30 PM // reply »
6,516 Comments

Thanks Chris!


Apr 10, 2007 at 12:52 PM // reply »
1 Comments

can i use a wildcard when comparing in coldfusion

for example

<cfif #variable# eq 'AE%'>


Apr 10, 2007 at 12:56 PM // reply »
6,516 Comments

You cannot use a SQL wild card when comparing values. However, you can use a regular expression and the REFind() function.

Take:

<cfif #variable# eq 'AE%'>

and make it into:

<cfif REFind( "^AE.+?", variable )>

The "^" means match at the beginning of the string.

AE is the literal string you are looking for.

.+? means at least ONE character after AE but don't bother checking more than one. You can probably remove this and JUST use "^AE", but it depends on what you want to do.

Regular expressions are totally awesome. If you want to know more about them, check out this primer:

http://www.bennadel.com/index.cfm?dax=blog:458.view


Apr 10, 2007 at 2:27 PM // reply »
24 Comments

@Justin,

CFML supports the CONTAINS operator:

<cfif variable CONTAINS 'AE'>

Just keep in mind that this operator is case insensitive, like all basic decision operators in CFML.

For more information, take a look at the docs: http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00000928.htm

Chris


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »