isNumeric() And numberFormat() Can Work With Very Large Numbers In Lucee CFML 5.3.6.61
For historical reasons, I'm a bit weary of any kind of numeric value that won't fit into a Signed Integer in ColdFusion. I can't point to specific issues necessarily; but, I know that I've run into errors working with large numbers. That said, yesterday when I was looking at how to implement an in-place natural sort of an alpha-numeric Array in Lucee CFML I discovered that the isNumeric()
and numberFormat()
functions can work fairly large strings. Though, upon closer examination, the numberFormat()
function is a little funny. As such, I wanted to put together a quick demo in Lucee CFML 5.3.6.61.
This demo is simple - I'm just taking a 1
and appending an increasing number of 0
's to it. Then, I'm taking the resultant value and running it through isNumeric()
and numberFormat()
:
<cfscript>
// Let's see how isNumeric() and numberFormat() work as we increase the
// order-of-magnitude to a large degree.
for ( i = 1 ; i < 50 ; i++ ) {
value = ( "1" & "0".repeatString( i ) );
echo( isNumeric( value ) );
echo( " " );
echo( numberFormat( value ) );
echo( "<br />" );
}
</cfscript>
Before running this, part of me would have expected it to throw a ColdFusion error the moment it encountered a number larger than 2,147,483,647
, which is the MAX_VALUE
property of the java.lang.Integer
class. However, when we run this ColdFusion code, we get the following output:
true 10
true 100
true 1,000
true 10,000
true 100,000
true 1,000,000
true 10,000,000
true 100,000,000
true 1,000,000,000
true 10,000,000,000
true 100,000,000,000
true 1,000,000,000,000
true 10,000,000,000,000
true 100,000,000,000,000
true 1,000,000,000,000,000
true 10,000,000,000,000,000
true 100,000,000,000,000,000
true 1,000,000,000,000,000,000
true 10,000,000,000,000,000,000
true 100,000,000,000,000,000,000
true 1,000,000,000,000,000,000,000
true 10,000,000,000,000,000,000,000
true 99,999,999,999,999,990,000,000
true 1,000,000,000,000,000,000,000,000
true 9,999,999,999,999,999,000,000,000
true 99,999,999,999,999,990,000,000,000
true 999,999,999,999,999,900,000,000,000
true 10,000,000,000,000,000,000,000,000,000
true 100,000,000,000,000,000,000,000,000,000
true 999,999,999,999,999,900,000,000,000,000
true 9,999,999,999,999,999,000,000,000,000,000
true 99,999,999,999,999,990,000,000,000,000,000
true 1,000,000,000,000,000,000,000,000,000,000,000
true 10,000,000,000,000,000,000,000,000,000,000,000
true 100,000,000,000,000,000,000,000,000,000,000,000
true 999,999,999,999,999,900,000,000,000,000,000,000
true 9,999,999,999,999,998,000,000,000,000,000,000,000
true 99,999,999,999,999,980,000,000,000,000,000,000,000
true 999,999,999,999,999,800,000,000,000,000,000,000,000
true 9,999,999,999,999,998,000,000,000,000,000,000,000,000
true 99,999,999,999,999,980,000,000,000,000,000,000,000,000
true 999,999,999,999,999,900,000,000,000,000,000,000,000,000
true 9,999,999,999,999,999,000,000,000,000,000,000,000,000,000
true 99,999,999,999,999,990,000,000,000,000,000,000,000,000,000
true 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
true 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
true 100,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
true 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
true 10,000,000,000,000,001,000,000,000,000,000,000,000,000,000,000,000
No errors! Well, no errors thrown at least. But, when we look closely, we can see that it performed well until:
10,000,000,000,000,000,000,000
... then, as the length of the input string continued to increase, we start to get some random 9
digits showing up. And, what's really odd is that if you look at the very last value, there a random 001
half-way through the output.
But, I'm not so worried about the random characters. The output only starts getting janky when you reach the sextillions (which is a term I had to look-up because I didn't know what came after trillions). As far as I'm concerned, this makes isNumeric()
and numberFormat()
pragmatically safe to use with arbitrarily large values in Lucee CFML 5.3.6.61.
Want to use code from this post? Check out the license.
Reader Comments