Multiplying percentage / getting percentage of int
-
Sunday, September 16, 2012 5:21 PM
Trying to find 25% of time2 - time1.
How do you find a percentage of an int?class Program { public const int minsInHour = 60; public const int hunPart = 100; public const double delay = .25; static void Main() { // Variable declarations int time1 = 0; int time2 = 0; int hours1 = 0; int hours2; int mins1 = 0; int mins2 = 0; int newTime = 0; string txt; while (true) { Console.WriteLine("Enter your departure time: "); txt = Console.ReadLine(); time1 = int.Parse(txt); mins1 = time1 % hunPart; hours1 = time1 /hunPart * minsInHour; if (time1 < 1441) break; } while (true) { Console.WriteLine("Enter your usual arrival time: "); txt = Console.ReadLine(); time2 = int.Parse(txt); mins2 = time2 % hunPart; hours2 = time2 /hunPart * minsInHour; if (time2 < 2401) break; } time1 = hours1 + mins1; time2 = hours2 + mins2; int trafficDelay = Convert.ToInt16(delay); newTime = time2 - time1 * trafficDelay; Console.WriteLine("{0}", time1); Console.WriteLine("{0}", time2); Console.WriteLine("{0}", newTime); Console.ReadLine();
thanks!!
All Replies
-
Sunday, September 16, 2012 5:38 PMModerator
Use a TimeSpan object to calculate elapsed time measurements.
TimeSpan Structure (.NET Framework 4)
It has a TotalMinutes property that returns a double.
static Double Subtract(DateTime dt1, DateTime dt2, double delay) { TimeSpan span = dt1 - dt2; return span.TotalMinutes * delay; }
Mark the best replies as answers. "Fooling computers since 1971."
http://thesharpercoder.blogspot.com/
- Edited by Rudedog2MVP, Moderator Sunday, September 16, 2012 5:49 PM
-
Sunday, September 16, 2012 5:49 PM
Thank you, but that doesn't answer my question.
I'm trying to display the result as an int, anyway.
Console.WriteLine("{0:d4}", newTime);
like so.
Thank you!
-
Sunday, September 16, 2012 5:52 PMModerator
static int Subtract(DateTime dt1, DateTime dt2) { TimeSpan span = dt1 - dt2; return Convert.ToInt32(span.TotalMinutes/4); }Mark the best replies as answers. "Fooling computers since 1971."
- Proposed As Answer by Lisa ZhuMicrosoft Contingent Staff, Moderator Tuesday, September 18, 2012 5:54 AM
- Marked As Answer by Lisa ZhuMicrosoft Contingent Staff, Moderator Friday, September 21, 2012 9:24 AM
-
Sunday, September 16, 2012 6:00 PMModerator
There are types in the .NET Framework that can perform time calculations.
http://msdn.microsoft.com/en-us/library/system.datetime.aspx
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."

