Solved

For loop in 15 min increments

Posted on 2014-11-26
6
203 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 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