2

I am trying to use cfloop to loop form 0.0 to 5.0 but it takes out the decimal point and is looping from 0 to 5 instead.

This is my code

<select name="cweight">

    <option value="">---</option>

    <cfloop index = "cweight" from = "0.0" to = "5.0"> 
        <option value="#cweightid#">#cweight#</option>
    </cfloop>

</select>

I need the loop to go over 0.1,0.2,0.3 until it reaches 5.0.

What should I add to allow me to do this?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Geo
  • 3,160
  • 6
  • 41
  • 82

1 Answers1

16

CF doesn't have "doubles" - numbers have decimal places when they need them.

To do what you want, use NumberFormat with the mask set to 0.0 so that you always get a decimal place.

To increment by 0.1 at a time, simply set the cfloop step attribute.

<cfloop index="cweight" from="0" to="5" step="0.1"> 
    <option value="#cweight#">#NumberFormat( cweight ,'0.0' )#</option>
</cfloop>
Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
  • Good answer! Clear and concise. – Evik James Aug 13 '12 at 17:08
  • Good answer for the first question but it does not really solve my issue. Thanks for the effort though. – Geo Aug 13 '12 at 17:24
  • How does it not solve your issue? It loops perfectly from 0.1,.2, etc.? – Busches Aug 13 '12 at 17:28
  • it was giving 1.0 2.0 3.0 and then I deleted all of my code and just copied this one and now its fine. Thanks for questioning me @Busches or else I wouldn't test it again since it didn't give me an error. – Geo Aug 13 '12 at 17:41
  • You forgot the step="0.1" part. – Busches Aug 13 '12 at 20:47
  • 1
    The original question didn't indicate they wanted 0.1 steps at all - that was edited in after I had answered, so I also edited my answer to mention step, then 18 minutes later the comment was posted about the answer not working - I guess they hadn't refreshed the page to see the update... – Peter Boughton Aug 13 '12 at 21:53
  • 1
    I know. :) Just trying to explain the mix-up. – Peter Boughton Aug 13 '12 at 22:59
  • I strongly recommend avoiding decimals in your STEPs... CF behavior is unpredictable. http://brien-malone-web-dev.blogspot.com/2014/04/coldfusion-10-cfloop-bug-with-decimal.html – Brien Malone Apr 10 '14 at 23:31