Solved

For loop in 15 min increments

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

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 32

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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