Skip to main content
Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.

isNumeric() And numberFormat() Can Work With Very Large Numbers In Lucee CFML

By Ben Nadel on
Tags: ColdFusion

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

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():


	// 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 />" );



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:


... 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

Reader Comments

What has two thumbs and hopes you leave a comment? This Guy! (Ben Nadel).

Post A Comment

You — Get Out Of My Dreams, Get Into My Blog
Live in the Now
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.