PC Review


Reply
Thread Tools Rate Thread

Casting double to int

 
 
Barry
Guest
Posts: n/a
 
      9th Feb 2005
What is the best way to round a double and store it in an int (C#)?
I'm currently using
intVar = (int)Math.Round(doubleVar);
I'm worried that the return value of e.g. Math.Round(2.1) might be
1.99999999999999999999999 (due to quirks of binary/decimal conversion),
and (int) will then truncate it to 2.
 
Reply With Quote
 
 
 
 
chris
Guest
Posts: n/a
 
      9th Feb 2005
You could use:

int number = (int)Math.Ceiling(doubleValue);

The smallest whole number greater than or equal to doubleValue.

int number = Convert.ToInt32(doubleValue);

rounds it to the nearest number.



Barry schrieb:
> What is the best way to round a double and store it in an int (C#)?
> I'm currently using
> intVar = (int)Math.Round(doubleVar);
> I'm worried that the return value of e.g. Math.Round(2.1) might be
> 1.99999999999999999999999 (due to quirks of binary/decimal conversion),
> and (int) will then truncate it to 2.

 
Reply With Quote
 
=?Utf-8?B?UElFQkFMRA==?=
Guest
Posts: n/a
 
      9th Feb 2005
> I'm worried that the return value of e.g. Math.Round(2.1) might be
> and (int) will then truncate it to 2.


Doesn't 2.1 round to 2 ?

How about:

int i = (int) (d+0.5)

Are you testing against a bunch of samples and seeing erroneous results? Or
are you perhaps worrying needlessly?
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      9th Feb 2005
chris wrote:
> You could use:
> int number = Convert.ToInt32(doubleValue);
>
> rounds it to the nearest number.


Thanks - just what I need
 
Reply With Quote
 
Bruno Jouhier [MVP]
Guest
Posts: n/a
 
      9th Feb 2005
ints are *exactly* representable as double, and longs are too when smaller
than 2^52 in absolute value.

So, Math.Round(someDecimal) will never be something like
someInt.99999999999999999999, it will be exactly someInt.000000000000000 and
the cast will always give the correct result.

But this is a special case, and most decimal numbers are *not* exactly
representable as doubles. So, you are right to be careful about this.

Bruno.

"Barry" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> What is the best way to round a double and store it in an int (C#)?
> I'm currently using
> intVar = (int)Math.Round(doubleVar);
> I'm worried that the return value of e.g. Math.Round(2.1) might be
> 1.99999999999999999999999 (due to quirks of binary/decimal conversion),
> and (int) will then truncate it to 2.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from a double to float -- please help! almurph@altavista.com Microsoft C# .NET 1 5th Mar 2009 01:56 PM
Casting to double in C# kanepart2@hotmail.com Microsoft C# .NET 14 9th Jun 2007 09:33 PM
Casting a datetime to double ?! chris-s@mailcity.com Microsoft Dot NET Compact Framework 2 28th Mar 2007 01:57 AM
Double.MaxValue Casting Problems =?Utf-8?B?S2V2aW4gQmxha2VsZXk=?= Microsoft Dot NET Framework 3 15th Feb 2007 10:01 PM
Casting a string to a double Tina Microsoft C# .NET 11 20th May 2006 07:59 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:36 AM.