Working with large numbers in ColdFusion

I just thought of sharing my friend's experience with managing large numbers using ColdFusion. They got an assignment that will manipulate numbers and things seem good at first. When the numbers started getting larger(20 digits), we had a problem. In CF, the limit of Integer is -2,147,483,648 and 2,147,483,647 (32-bit signed integers) and the calculations started giving unexpected results.
After quite a bit of Googling and posting in forums, they got the solution from a user in the Adobe forums. It is to use the Java Math object and convert the number to BigInteger. Here is the code:
<cfset N1= createobject("java","java.math.BigInteger").init("1")>
<cfset N2 = createobject("java","java.math.BigInteger").init("9999999999999899999999")>
<cfset RESULT = N1.add(N2).toString()>
<cfoutput>#result#</cfoutput>
This code worked perfect.

I have earlier blogged about how to use Jar files in CF, but haven't really studied Java in detail. Seeing this, I think I should study Java in more detail so that I could find a better and easier solutions in such situation. 

Comments

Popular posts from this blog

Hide notification content in Android lock screen for Messages, email, whatsapp and selected apps.

Array functions in JavaScript