Cold Fusion is a typeless language, meaning a variable can contain any kind of value (numeric, string, boolean, etc.). This recently caused me much confusion until someone on the Macromedia CF Forum pointed this out to me.
I had an application where I wanted to display a price value. If the price value had no cents, but the user typed in an ending ".00", I wanted to remove this, and just display the whole dollar amount (e.g. $25 instead of $25.00). So being the smart programmer, I just looked for a ".00" as the last three characters and dropped them if it existed. I tested it, and if worked just as desired.
The first day in production, someone calls and says they keep entering $6,000 but it shows up on the web as "$6,". What!! I test this myself, and yep, the last three zeros are getting dropped. I scour the code, write a little test, and CF keeps thinking ".00" is equal to "000". Of course any human can see these strings are not equal.
They may look like unequal strings to me, but to CF they're the same. Why? Well, CF looks at the first ".00" and decides that this can be interpreted as a numeric ZERO, so that's how it treats it, as numeric. Then the "000" can be interpreted as a numeric ZERO too, so ZERO equals ZERO and those last three zeros are going to be dropped from the value and "$6," is going to be displayed.
The solution is to use the compare() function, which does a string comparison.
Incorrect:
<cfif right(trim(itm_cost),3) eq ".00">
<cfset form.itm_cost=left(trim(form.itm_cost),
len(trim(form.itm_cost)) - 3)>
</cfif>
Correct:
<cfif NOT Compare(right(trim(itm_cost),3),".00")>
<cfset form.itm_cost=left(trim(form.itm_cost),
len(trim(form.itm_cost)) - 3)>
</cfif>
I know in the future I'll come across this again and can only hope I remember the "typeless" nature of Cold Fusion.
Posted at March 29, 2004 08:07 PM