Answered by:
Accurate Integer part from double number

Ho do I get an accurate integer part from a double number?
I tried that from Immediate Window in VS2005, and I got these results:
?Math.Floor(3.9999999999999999)
4.0
?Math.Floor(3.999999999999999)
3.0
I'm expecting the function to return a 3 (integer value would be better) in both cases.
Thanks,
yosoyelkoch
Question
Answers

Doubles do not store infinite precision. It is not possible for a double value to know the difference between your first example and 4.0.
Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
and How to Implement IDisposable and Finalizers: 3 Easy Rules
Microsoft Certified Professional Developer
How to get to Heaven according to the Bible Marked as answer by Harry Zhu Friday, June 04, 2010 6:44 AM

Do you want it rounded or truncated?
Assume double value is in a variable named input and the integer will be put in a variable named final:
If you want it truncated, just cast it.
final = (int)input;
If you want it rounded,
final = Math.Round(input, 0);
 Proposed as answer by GonzoKnight Friday, May 28, 2010 5:22 PM
 Marked as answer by Harry Zhu Friday, June 04, 2010 6:44 AM

Welcome to the MSDN Forums.
Did you mean following?
double d = 25.37; int i = (d > 0 ? (int)Math.Floor(d) : (int)Math.Ceiling(d)); double d2 = (double)(Convert.ToDecimal(d)  i);
Thank you.
WHAT'S NEW IN THE .NET FRAMEWORK 4:
Article: Comparison of parallel and sequential computing in .NET Framework Marked as answer by Harry Zhu Friday, June 04, 2010 6:44 AM

As pointed out, double is not able to distinguish between 3.9999999999999999 and 4.0, just calculate the difference and your result will be 0.
If you are dealing with money or other values where it is important that 3. 9999999999999999 is exactly this and not a binary approximation of it then you should use the decimal data type (which behind the scenes is an integer type).
What you mean by accurate in your question has no definite answer, it depends on where that number came from: if it is a result from a floating calculation, your Floor() result of 4.0 is as accurate as it can be (difficult to imagine a calculation with so many significant digits though :)
Cristian.
 Marked as answer by Harry Zhu Friday, June 04, 2010 6:44 AM
All replies

Doubles do not store infinite precision. It is not possible for a double value to know the difference between your first example and 4.0.
Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
and How to Implement IDisposable and Finalizers: 3 Easy Rules
Microsoft Certified Professional Developer
How to get to Heaven according to the Bible Marked as answer by Harry Zhu Friday, June 04, 2010 6:44 AM

Do you want it rounded or truncated?
Assume double value is in a variable named input and the integer will be put in a variable named final:
If you want it truncated, just cast it.
final = (int)input;
If you want it rounded,
final = Math.Round(input, 0);
 Proposed as answer by GonzoKnight Friday, May 28, 2010 5:22 PM
 Marked as answer by Harry Zhu Friday, June 04, 2010 6:44 AM

Sorry, I want it truncated. That's why I told I'm expecting 3 in both Cases. Looks like (int) casting works exactly as Math.Floor, except for the function returns a double but the casting is returning an integer.
?Math.Floor(3.999999999999999)
3.0
?Math.Floor(3.9999999999999999)
4.0
?(int)3.9999999999999999
4
?(int)3.999999999999999
3
Thanks,
yosoyelkoch 
Welcome to the MSDN Forums.
Did you mean following?
double d = 25.37; int i = (d > 0 ? (int)Math.Floor(d) : (int)Math.Ceiling(d)); double d2 = (double)(Convert.ToDecimal(d)  i);
Thank you.
WHAT'S NEW IN THE .NET FRAMEWORK 4:
Article: Comparison of parallel and sequential computing in .NET Framework Marked as answer by Harry Zhu Friday, June 04, 2010 6:44 AM

As pointed out, double is not able to distinguish between 3.9999999999999999 and 4.0, just calculate the difference and your result will be 0.
If you are dealing with money or other values where it is important that 3. 9999999999999999 is exactly this and not a binary approximation of it then you should use the decimal data type (which behind the scenes is an integer type).
What you mean by accurate in your question has no definite answer, it depends on where that number came from: if it is a result from a floating calculation, your Floor() result of 4.0 is as accurate as it can be (difficult to imagine a calculation with so many significant digits though :)
Cristian.
 Marked as answer by Harry Zhu Friday, June 04, 2010 6:44 AM