Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

For loop in 15 min increments

Posted on 2014-11-26
6
204 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

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…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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