We help IT Professionals succeed at work.

How do I divide the time by the dollar amount?  ASP.NET C#

bmanmike39
bmanmike39 asked
on
How do I divide the  time object by the cost


code:
TimeSpan ts = new TimeSpan();
        ts = date1 - date2;

string time = ts.ToString();
        
        string cost = "35.00";
        
        decimal ad;
        
        ad = decimal.Parse(cost);

Open in new window

Comment
Watch Question

Commented:
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.

Commented:
I should have said ts.TotalSeconds or ts.TotalMinutes or even ts.TotalHours

Author

Commented:
Something like this?  I don't know how to do this.

TimeSpan ts = new TimeSpan();
        ts = date1.Minute() - date2.Minute;

Author

Commented:
I changed this line to
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 );

Open in new window

Author

Commented:
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’


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.

Author

Commented:
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’
Commented:
You're pretty close just cant convert the double from your division into a string which the label needs.

        DateTime date1 = DateTime.Now;
        DateTime date2 = DateTime.Now.AddHours(-10);
       
        TimeSpan ts = new TimeSpan();

        ts = date1 - date2;

        string time = ts.TotalMinutes.ToString();

        double cost = double.Parse("35.00");

        //label9.Text = String.Format("Cost({0}) / Minutes({1}) = {2}", cost.ToString(), ts.TotalMinutes.ToString(), (cost / ts.TotalMinutes).ToString("C"));

        label9.Text = String.Format("Cost({0}) / Minutes({1}) = {2}", cost.ToString(), ts.TotalMinutes.ToString(), (cost / ts.TotalMinutes).ToString());



OR JUST:

Label9.Text = (cost / ts.TotalMinutes).ToString();

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" );

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.