Comparison of a large number of ColdFusion

ColdFusion counts 10090000000557765= 10090000000557763.

I understand why, but I need to know how ColdFusion can better understand that they DO NOT match. I read about the method Compare(), but also returns true. I passed them as strings and also returned true.

I was thinking of writing a custom function to split the string into two parts, compare them individually and then return true / false, but that seems dumb.

I tried the following:

Val(a) EQ Val(b)
ToString(a) EQ ToString(b)
a.compareTo(b)

For clarification. I use DB, which I do not control, which uses bigint. I found out at an early stage that Javascript was not able to process them, so I converted all the bigint fields to varchar in my models. Now, however, CF has the problem of comparing strings, and I cannot convert them back to numbers.

An example I just made:

<cfif '10090000000557765' EQ '10090000000557763'>
True
<cfelse>
False
</cfif>

At http://cflive.net/ and it came back. See My Note. They are large in the database. I had to throw them as VarChar when I pulled them out because Javascript cannot handle bigint, but it just builds the lines.

+4
source share
2 answers

The short answer to not miss obvious, java-methods such as Long.compareTo () return -1, 0, or 1. Make sure you use the correct expression to check for equality. The result will be 0when the values ​​are equal. (Zero (0) evaluates falseto CF).

: , , . , , CF11 ( val, ). , - .

, CF11 SQL Server:

CREATE TABLE test
(
    bigIntValue1 BIGINT
    , bigIntValue2 BIGINT
    , varcharValue1 VARCHAR(50)
    , varcharValue2 VARCHAR(50)
)

:

writeOutput("<br>qry.bigIntValue1: "&  val(qry.bigIntValue1) );
writeOutput("<br>qry.bigIntValue2: "&  val(qry.bigIntValue2) );
writeOutput("<br>BigInt Val(): "& ( val(qry.bigIntValue1) eq val(qry.bigIntValue2)) );
writeOutput("<br>Varchar Val(): "& ( val(qry.varcharValue1) eq val(qry.varcharValue2)) );
writeOutput("<br>Long.compareTo: "& qry.bigIntValue1[1].compareTo(qry.bigIntValue2[1]));
writeOutput("<br>Varchar.compareTo: "& qry.varcharValue1[1].compareTo(qry.varcharValue2[1]));
writeOutput("<br>Compare(Long, Long) "& compare(qry.bigIntValue1, qry.bigIntValue2));
writeOutput("<br>Compare(Varchar, Varchar) "& compare(qry.varcharValue1, qry.varcharValue2));

:

val(qry.bigIntValue1):     1.00900000006E+016
val(qry.bigIntValue21):    1.00900000006E+016
BigInt Val():              YES
Varchar Val():             YES

Long.compareTo:            1
Varchar.compareTo:         2
Compare(Long, Long):       1
Compare(Varchar, Varchar): 1 

, val() , . .

, . , : 0, . , . (0) false CF, , .

+10

. , .

<cfset x = 10090000000557765/>
<cfset y = 10090000000557763/>
<cfset isZero = PrecisionEvaluate( x-y )/>
<cfif isZero EQ 0>
   x and y are equal
<cfelse>
   x and y are not equal
</cfif>

(, int, longInt ..), . PrecisionEvaluate().

+3

All Articles