Solved

For loop in 15 min increments

Posted on 2014-11-26
6
215 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
[X]
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
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 34

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

695 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