All numeric data types in CF are typed as Java Strings. You must do a
JavaCast() to type BigDecimal and do comparisons on that. Here is your
modified code:
<cffunction name="isValidFloatRange" hint="is passed a value, a min
range, and a max range and checks to ensure the value is in between the
2 ranges">
<cfargument name="value" type="numeric" required="true">
<cfargument name="minRange" type="numeric" required="true">
<cfargument name="maxRange" type="numeric" required="true">
<cfset var xvalue = javacast("bigdecimal", ARGUMENTS.value) />
<cfset var xminRange = javacast("bigdecimal", ARGUMENTS.minRange) />
<cfset var xmaxRange = javacast("bigdecimal", ARGUMENTS.maxRange) />
<cfif xminRange.compareTo(xmaxRange) GTE 0>
<cfreturn "Error in Range: #maxRange# must be larger than
#minRange#)" />
</cfif>
<cfif (xvalue.compareTo(xminRange) EQ -1) or
(xvalue.compareTo(xmaxRange) EQ 1)>
<cfreturn "Float out of range: #value# (Valid Float Range:
#minRange#
to #maxRange#)" />
<cfelse>
<cfreturn true>
</cfif>
</cffunction>
<cfoutput>
#isValidFloatRange(1.9999999999999999999999999999999,1,3)#<br />
#isValidFloatRange(1,1,3)#<br />
#isValidFloatRange(2,1,3)#<br />
#isValidFloatRange(2.99999999999999999999999999999999999,1,3)#<br />
#isValidFloatRange(3.00000000000000000000000000001,1,3)#<br />
#isValidFloatRange(0.9999999999999999999999999999999999999999999,1,3)#<br
/>
#isValidFloatRange(1.000000000111111111111111111111111111111111111,1,3)#<br
/>
</cfoutput>
Make sure to VAR your functions, and I've set a validation against
switched max/min Ranges (or you can just switch them)
Richard White wrote:
> hi,
>
> i have a function that is passed a float value, a min float range, and a max
> float range and it compares whether the float is in the correct range and
> passes back true or an error:
>
> <cffunction name="isValidFloatRange" hint="is passed a value, a min range,
> and a max range and checks to ensure the value is in between the 2 ranges">
> <cfargument name="value" type="numeric" required="true">
> <cfargument name="minRange" type="numeric" required="true">
> <cfargument name="maxRange" type="numeric" required="true">
>
> <cfif value lt minRange or value gt maxRange>
> <cfreturn "Float out of range: #value# (Valid Float Range: #minRange# to
> #maxRange#)">
> <cfelse>
> <cfreturn true>
> </cfif>
>
> </cffunction>
>
> however, it all seems to work fine until i put in the following criteria:
>
> <cfoutput>#isValidFloatRange(1.9999999999999999,2.00,3.00)#</cfoutput>
>
> for some reason it returns true saying that value is between 2 and 3. but if
> i take one of the 9's out of the value then it produces the required error
> saying it is not inbetween the range
>
> has anyone come across this before or see if there is something i am missing
>
> thanks
>
> richard
>
>
>
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to
date
Get the Free Trial
http://ad.doubleclick.net/clk;207172674;29440083;f
Archive:
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:313588
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4