Copy link to clipboard
Copied
Recently we have seen an issues with number having 16 digits.
For eg val(9999999999999399) internally getting coverted to some other number. In this case its 9999999999999400.
Because of which we are facing a lot of validation issues and challenges.
Is there a way to handle such huge number in coldfusion.
The other workaround is to convert it to string but wated to check with all the possibility before doing so.
Thanks!
Copy link to clipboard
Copied
The other workaround is to convert it to string but wated to check with all the possibility before doing so.
By @Dakshata28347643aeno
Converting the argument to string before passing it to the val function - that is in fact the recommended way to do it. 🙂
The reason being that val is a string function. Which is why the following code will work just fine:
<cfoutput>#val("9999999999999399abcd")#</cfoutput>
Anyway, in Java, the engine ColdFusion runs on, the range for an integer is from -2147483648 to 2147483647. That is a span of 10 digits. Your number, 9999999999999399 , is way out of that range. So, by estimating 9999999999999399 as 9999999999999400, ColdFusion is actually doing you a favour!
To answer your question, yes there are ways to handle such huge numbers. A common way is to use the Java class java.math.BigInteger. If you do, then you will need to use BigInteger's methods to perform arithmetical operations.
For example, you will have to:
<cfset m=9999999999999399>
<cfset bigM=createobject("java","java.math.BigInteger").init(m)>
<p>
bigM as string: <cfoutput>#bigM.toString()#</cfoutput>
</p>
<cfset n=8888888888888388>
<cfset bigN=createobject("java","java.math.BigInteger").init(n)>
<p>
bigN as string: <cfoutput>#bigN.toString()#</cfoutput>
</p>
<!--- Addition --->
<cfset bigMplusBigN=bigM.add(bigN)>
<p>
bigMplusBigN as string: <cfoutput>#bigMplusBigN.toString()#</cfoutput>
</p>
<!--- Subtraction --->
<cfset minusBigN=bigN.negate()>
<cfset bigMminusBigN=bigM.add(minusBigN)>
<p>
bigMminusBigN as string: <cfoutput>#bigMminusBigN.toString()#</cfoutput>
</p>
Copy link to clipboard
Copied
@Dakshata28347643aeno , did that help?
Copy link to clipboard
Copied
Dakshata, besides what BKBK has offered, I want to ask first about the screenshot image you attached, showing that one line of code (<cfoutput>#val(9999999999999399)#</cfoutput>) and outputting that value you show, 9999999999999400.
We can tell it's from the trycf.com site, but did you know that the code is running against Lucee, not CF? If you change the option on that site to run this against CF(2021 or 2018), it shows the output instead to be 1E+016. Same if you use Adobe's cffiddle.org site (which only supports CF, not Lucee).
Perhaps as important, if you put that number into a variable such as x and writeoutput(x)--without the val--that shows the actual number (not changed in anyway), and that's true in either CF or Lucee. So if indeed outputting is the problem for you, and the val changes things, can you clarify why want or feel you need to use the val? I realize maybe your "real" code is more involved.
But maybe your real issue NOT about what is in the cfoutput. In that case, could you take a moment to code up even a simple demonstration of what is that real issue?
And then run it in either trycf.com and be sure to choose CF, or use cffiddle.org (which only offers CF versions)? BTW, neither tool "remembers" what engine you choose between visits (though they do keep the engine choice set as you make changes and re-submit them in a current visit).
Copy link to clipboard
Copied
Here is a short answer to your post:
<!--- The argument to the function val should be a string. So this code is, strictly speaking, incorrect. --->
<cfset x=val(9999999999999399)>
<!--- The correct version. --->
<cfset x=val("9999999999999399")>
In any case, as Charlie has said, you will get 100% accuracy if you leave out "val" and simply output 9999999999999399. The reason is that, during output, ColdFusion casts simple-values to string.
However, accuracy comes into play the moment you perform any operations on numbers . Including the use of "val". You then have to reckon with Java's number types. For example:
int | 32 bits | -2,147,483,648 - 2,147,483,647 |
long | 64 bits | -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 |
Your number 9999999999999399 is a Long. To perform operations on numbers that big, you will need techniques from Java, such as BigInteger.