Hi i would like to know how to get rid of of the decimals if i have a number eg: 2.56 changed to: 2.
i've tried the Math.Round() but it rounds to the nearest 10 I just want the decimals away. I tried changing the double to an int variable. I probably dont know how cause I got stuck trying.
I know this is a simple problem to solve I dont know y im taking stuck with it. Any ideas?
thanks
AlanPosted Oct 3, 2007, 6:31 AM
In that case, you could use either of the following which both round towards zero (so -2.56 rounds to -2):
double d = 2.56;
d = (double)Decimal.Truncate((decimal)d);
// OR (better):
d = (double)(long)d;
or you could use this which rounds to the lower integer (so -2.56 rounds to -3):
double d = 2.56;
d = Math.Floor(d);
Felipe MarksPosted Oct 3, 2007, 5:55 AM
AlanPosted Oct 3, 2007, 5:00 AM
If you're using .NET 2.0 or greater, I'd use the Math.Truncate() method for this:
http://msdn2.microsoft.com/en-us/library/c2eabd70(vs.80).aspx