N. Sch
N. Sch

Reputation: 687

How to do calculations price calculation based on time and wage

I am trying to figure out final price based on wage multiplied by hours.

Here is my code Why if the hours are 02:30:00 and the wage: $14 is my total 2?

TimeSpan duration = TimeSpan.Parse("02:30:00"); 
int wage = 14;
var result = (duration.Hours + (duration.Minutes / 60) * wage);

Upvotes: 3

Views: 1151

Answers (1)

shree.pat18
shree.pat18

Reputation: 21757

First, the expression is actually evaluated as:

duration.Hours + ((duration.Minutes / 60) * cleaner.Price)

Second, you are doing integer division, so 30/60 will result in 0, leaving you with the value 2 i.e. the Hours part.

To fix the issue, you can do something like the below:

(duration.Hours + ((decimal)duration.Minutes / 60)) * 14;

Alternatively, you can skip this component-wise calculation, and just use the TotalHours property instead:

duration.TotalHours * 14

Upvotes: 3

Related Questions