tags:

views:

947

answers:

5

Hi All, I need to get the left hand side integer value from a decimal or double. For Ex: I need to get the value 4 from 4.6. I tried using Math.Floor function but it's returning a double value, for ex: It's returning 4.0 from 4.6. The MSDN documentation says that it returns an integer value. Am I missing something here? Or is there a different way to achieve what I'm looking for? Thank you.

+7  A: 

According to MSDN, Math.Floor(double) returns a double: http://msdn.microsoft.com/en-us/library/e0b5f0xb.aspx

If you want it as an int:

int result = (int)Math.Floor(yourVariable);

I can see how the MSDN article can be misleading, they should have specified that while the result is an "integer" (in this case meaning whole number) it is still of TYPE Double

Neil N
Thank you for your reply. Actually I was looking at this article: http://msdn.microsoft.com/en-us/library/e0b5f0xb.aspxBut anyways, I will try your suggestion. Thank you.
Well true, it does say at the top "returns an integer" but the type is specified below it: public static double Floor(double d)
Neil N
integer != `int` (http://www.answers.com/integer) - integers *can* be stored in `Double` (see Jon's answer), and there are an infinite number of integers that cannot be stored in an `int`.
Shog9
Shog, I didnt say they couldn't. What I said was "it is still of TYPE Double"
Neil N
@Neil: right - just wanted to stress the difference between "integer" (name of a set) and `int` (name of a type).
Shog9
A: 

That is correct. Looking at the declaration, Math.Floor(double) yields a double (see http://msdn.microsoft.com/en-us/library/e0b5f0xb.aspx). I assume that by "integer" they mean "whole number".

Jon
A: 

Floor leaves it as a double so you can do more double calculations with it. If you want it as an int, cast the result of floor as an int. Don't cast the original double as an int because the rules for floor are different (IIRC) for negative numbers.

plinth
+18  A: 

The range of double is much wider than the range of int or long. Consider this code:

double d = 100000000000000000000d;
long x = Math.Floor(d); // Invalid in reality

The integer is outside the range of long - so what would you expect to happen?

Typically you know that the value will actually be within the range of int or long, so you cast it:

double d = 1000.1234d;
int x = (int) Math.Floor(d);

but the onus for that cast is on the developer, not on Math.Floor itself. It would have been unnecessarily restrictive to make it just fail with an exception for all values outside the range of long.

Jon Skeet
double is wider than the range of double? Think you meant long. :)
Andrew Coleson
Floor returns integer representation of double, and it returns double for scientific calculations, this answer is not correct because double has 64 bits and long also has 64 bits, but double can not store exact digits of lower significant bits even if it can be stored correctly in long.
Akash Kava
@Jon: how come you haven't weighed in on the debate raging over how to make a positive number negative in C#?: http://stackoverflow.com/questions/1348080/convert-a-positive-number-to-negative-in-c
MusiGenesis
@Andrew: Fixed, thanks. @MusiGenesis: I'm browsing over flaky GPRS; it's painful as hell. This will be my only answer today. @Akash: Which bit of my answer do you consider incorrect? Just because both `long` and `double` use the same number of bits doesn't mean they have the same range. Look at `double.MaxValue-double.MinValue` and `long.MaxValue-long.MinValue`... There are integers which are in the range of `double` (even if they aren't all exactly representable) but outside the range of `long`. My example shows such an integer.
Jon Skeet
@Jon: take your time. It turns out the community discovered that multiplying a positive number by -1 will make it negative. All is well on StackOverflow.
MusiGenesis
+1  A: 

If you just need the integer portion of a number, cast the number to an int. This will truncate the number at the decimal point.

double myDouble = 4.6;
int myInteger = (int)myDouble;
Jon Seigel