Solved

For loop in 15 min increments

Posted on 2014-11-26
6
199 Views
Last Modified: 2016-02-17
Hi I am trying to run a for loop to generate a List<String>  for a dropdown list that will allow users to select a time value based on 15 min increments
.25, .50, .75, 1.0, 1.25   etc...

this is what I have so far but it keeps returning "0" 's

protected List<String> fill_ddlDuriation()
        {
            List<String> lstDuration = new List<String>();
            for(int i = 0; i < 720; i += 15)
            {
                i = i / 60;
                lstDuration.Add(i.ToString());
            }
                return lstDuration;
        }

Open in new window

0
Comment
Question by:Scarlett72
6 Comments
 
LVL 13

Assisted Solution

by:Philip Pinnell
Philip Pinnell earned 100 total points
ID: 40467182
try


protected List<String> fill_ddlDuriation()
    {
      List<String> lstDuration = new List<String>();
      for (decimal i = 0; i < 720; i += 15)
      {
        i = i / 60;
        lstDuration.Add(i.ToString());
      }
      return lstDuration;
    }

you won't get fractions in an integer variable
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 300 total points
ID: 40467185
It's because of line 6

i = i / 60;
               
You are looping by i (line 4). So first time, i = 15.

Then i get divided by 60, so i = 15/60 = 0.25 (rounded down to 0).

That figure gets carried forward.

Instead of using i in lines 6 and 7, use j:

j = i / 60;
lstDuration.Add(j.ToString());

(Make sure j is a decimal, not an integer)
0
 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 100 total points
ID: 40467194
Your loop variable (i) is defined as an integer. When you divide an integer by an integer, your result will be an integer, especially when you are storing the result in an integer.

Store your result in a double and cast the two parts of the division to doubles:

double result = (double) i / 60.0;
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 33

Expert Comment

by:it_saige
ID: 40467339
You could also use linq:
using System;
using System.Linq;
using System.Collections.Generic;

namespace EE_Q28570067
{
	class Program
	{
		private readonly static List<double> durations = new List<double>();

		static void Main(string[] args)
		{
			durations.AddRange((from i in Enumerable.Range(0, 720) where i % 15 == 0 select ((double)i / 60f)));
			foreach (var duration in durations)
				Console.WriteLine(string.Format("Interval: {0}", duration));
			Console.ReadLine();
		}
	}
}

Open in new window


Produces the following output:Capture.JPG
-saige-
0
 

Author Closing Comment

by:Scarlett72
ID: 40467396
Hi below was my solution, thanks to all for your feedback, not very familiar with linq syntax it_saige, but thanks for your input.

protected List<String> fill_ddlDuriation()
        {
            List<String> lstDuration = new List<String>();
            for(int i = 15; i < 735; i += 15)
            {
                Decimal minResult = (i*100) / 60;
                minResult = Math.Truncate(minResult) / 100;    
                lstDuration.Add(minResult.ToString("F"));
            }
                return lstDuration;
        }
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 40468558
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A short film showing how OnPage and Connectwise integration works.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now