1

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?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72

2 Answers2

2

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.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Dan Roberts
  • 4,664
  • 3
  • 34
  • 43
  • 1
    @Alex he is running a val() on the value before appending. https://cfdocs.org/val – Dan Roberts Oct 16 '19 at 19:51
  • `val` maybe converts to number (removes characters) but the array still stores as a `variant` type. array['numeric'] eventually stores as double. When you use `cfqueryparam cf_sql_numeric` you see in the debug output that all numbers are passed as float/double into the database. – Bernhard Döbler Oct 17 '19 at 14:07
0

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.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72