tags:

views:

46

answers:

3
double d = toDateTime.SelectedDateTime.Subtract(
              servicefromDateTime.SelectedDateTime).TotalHours;             
string s = String.Format("{0:0}",d); 

But the String.Format rounds up the value: if d is 22.91 the String.Format gives the rounding result of 23. I don't want to round up. For example, if d is 22.1222222, then I want 22. if d is 22.999999, then I want 22.

How can I achieve this?

+1  A: 

If you cast the double to an int/long it will chop off any decimal component, effectively giving you a "floor" or round-down of the double.

Andy White
+3  A: 

Then you need to Math.Floor

double d = toDateTime.SelectedDateTime.Subtract(servicefromDateTime.SelectedDateTime).TotalHours;

string s = String.Format("{0:0}",Math.Floor(d)); 
Jamiec
Floor would not work for negative numbers. Math.Floor(-7.1) returns -8.
Chris Taylor
+1  A: 

You could use Math.Truncate

double d = toDateTime.SelectedDateTime.Subtract(servicefromDateTime.SelectedDateTime).TotalHours; 

string s = String.Format("{0:0}", Math.Truncate(d));
Chris Taylor
I couldn't say which behaviour the OP wanted - but one of Math.Floor or Math.Truncate will indeed do what he wants.
Jamiec