Skip to main content
Known Participant
June 24, 2015
Question

How do I use cfloop to insert numbers that are not rounded?

  • June 24, 2015
  • 3 replies
  • 335 views

I ran the following code and it displayed my numbers correctly, ie: 1.75, 2.50:

<cfloop from="1" to="#getnumba#" index="idx">

    <cfset getunit = form["unit" & idx]>

<cfoutput>#NumberFormat(getunit, "99.99")#</cfoutput><br />

</cfloop>

<cfabort>

But when I tried the following code, it rounded the numbers again:

<cfloop from="1" to="#getnumba#" index="idx">

               <cfset getqty = form["qty" & idx]>

    <cfset getitem = form["item" & idx]>

    <cfset getunit = form["unit" & idx]>

              

<cfif (getqty is "") and (getitem is "") and (getunit is "")>

               <cfset getqty = 0>

    <cfset getitem = 0>

    <cfset getunit = 0>

</cfif>  

              

<cfquery name="insertItems" datasource="#application.dsn#">

INSERT into items

(orderID,

               qty,

               item,

               unit

)

VALUES(

<cfqueryparam value="#getoid#" CFSQLType="CF_SQL_INTEGER" >,

<cfqueryparam value="#getqty#" CFSQLType="CF_SQL_INTEGER" >,

<cfqueryparam value="#getitem#" CFSQLType="CF_SQL_VARCHAR" >,

<cfqueryparam value="#NumberFormat(getunit, "99.99")#" CFSQLType="CF_SQL_INTEGER" >

)

</cfquery>

</cfloop>

    This topic has been closed for replies.

    3 replies

    Known Participant
    June 29, 2015

    Thank you gentlemen, the problem has been solved. I don't know why I wasn't notified of your responses.

    USawIt
    Participant
    June 24, 2015

    As mentioned by Steve, above, CF_SQL_INTEGER is causing the issue. Try CFSQLType="CF_SQL_DOUBLE" or CFSQLType="CF_SQL_FLOAT", or CF_SQL_NUMERIC and CF_SQL_DECIMAL. by adding the additional attribute, scale=1,2,3.... [scale => Number of decimal places you want passed in parameter. Applies to CF_SQL_NUMERIC and CF_SQL_DECIMAL].


    For example:

    <cfqueryparam value="#NumberFormat(getunit, "99.99")#" CFSQLType="CF_SQL_DECIMAL" scale="2" >

    Legend
    June 24, 2015

    Just a guess but in your cfqueryparam you are specifying CFSQLType="CF_SQL_INTEGER".