bmanmike39
asked on
How do I divide the time by the dollar amount? ASP.NET C#
How do I divide the time object by the cost
code:
code:
TimeSpan ts = new TimeSpan();
ts = date1 - date2;
string time = ts.ToString();
string cost = "35.00";
decimal ad;
ad = decimal.Parse(cost);
I think the key to the answer is my question about your time unit. You cannot answer the question unless you know what unit of time, day, hour, seconds or? You need to convert the time from being an interval in Time units to a time period in the unit you want for your answer and then divide by dollar amount. I suspect you don't want the cost per tick unless you really care about the cost per 100 nanoseconds. If you want seconds, try ts.seconds or ts.minutes for minutes.
I should have said ts.TotalSeconds or ts.TotalMinutes or even ts.TotalHours
ASKER
Something like this? I don't know how to do this.
TimeSpan ts = new TimeSpan();
ts = date1.Minute() - date2.Minute;
TimeSpan ts = new TimeSpan();
ts = date1.Minute() - date2.Minute;
ASKER
I changed this line to
string time = ts.TotalMinutes.ToString() ;
but how do i do the divide the time by cost?
string time = ts.TotalMinutes.ToString()
but how do i do the divide the time by cost?
I guess you need something like this:
DateTime date1 = DateTime.Now;
System.Threading.Thread.Sleep( new Random().Next( 500, 2500 ) );
DateTime date2 = DateTime.Now;
TimeSpan ts = date2 - date1;
double cost = 35.0;
Console.WriteLine( "{0:F2} $/sec", cost / ts.TotalSeconds );
ASKER
Yes, basically. I tried:
Label9.Text = cost / ts.TotalMinutes);
but i get he error:
Operator “/” can not be applied to operands of type ‘string’ and ‘double’
Label9.Text = cost / ts.TotalMinutes);
but i get he error:
Operator “/” can not be applied to operands of type ‘string’ and ‘double’
double d = double.Parse( "35.00" ) could work for you. This could be useful in case the amount is entered on the form, in a text box control.
ASKER
Tried this:
string time = ts.TotalMinutes.ToString() ;
double cost = double.Parse("35.00");
Label9.Text = cost / ts.TotalMinutes;
Got this error:
Can not implicitly convert type ‘double’ to ‘string’
string time = ts.TotalMinutes.ToString()
double cost = double.Parse("35.00");
Label9.Text = cost / ts.TotalMinutes;
Got this error:
Can not implicitly convert type ‘double’ to ‘string’
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
string time = ts.TotalMinutes.ToString(); // no need to convert TotalMinites to string: double time = ts.TotalMinutes;
double cost = double.Parse("35.00");
Label9.Text = cost / ts.TotalMinutes; // this will work, but you need to round the result up to cents:
Label9.Text = ( cost / ts.TotalMinutes ).ToString( "F2" );