James A Mohler
James A Mohler

Reputation: 11120

Typed array vs untyped array in ColdFusion

I have the following code:

<cfscript>
data = ['2342bas', 'asqwerewq', '12314', 12421, 1.1];

newdata = arrayNew['Numeric'](1);

for (item in data)  {
    newdata.append(val(item));
    }

writedump(newdata); 


newdata = [];

for (item in data)  {
    newdata.append(val(item));
    }

writedump(newdata);  
</cfscript>

I am getting the following results:

enter image description here

I am wondering why they are different. Does 'Number' force all the data to be floating point?

Upvotes: 1

Views: 136

Answers (2)

James A Mohler
James A Mohler

Reputation: 11120

To get the answer I had to dive into the meta data

<cfscript>
data = ['2342bas', 'asqwerewq', '12314', 12421, 1.1];

newdata = arrayNew['Numeric'](1);

for (item in data)  {
    newdata.append(val(item));
    }

newdata.each(function(value) {
    writeoutput("<br /><b>#value#</b> #getMetadata(value).getName()#");
    });

writeoutput("<hr />");


newdata = [];

for (item in data)  {
    newdata.append(val(item));
    }

newdata.each(function(value) {
    writeoutput("<br /><b>#value#</b> #getMetadata(value).getName()#");
    });
</cfscript>

Results

enter image description here

It is interesting that BigDecimal always has a decimal and Double may or may not. Based on this question, ColdFusion: Get variable type , I never knew their was a way to use a BigDecimal in ColdFusion.

Upvotes: 0

Dan Roberts
Dan Roberts

Reputation: 4694

ColdFusion often has numeric values as java.lang.Double data types. It is likely doing a cast on each value to java.lang.Double as part of the append.

Upvotes: 2

Related Questions