Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 10th, 2008, 08:55 PM
Curious
Guest
 
Posts: n/a
Default Isssue with converting double to int

Hi,

I have a double number and need to convert it to an int. Unfortunately
it simply drops the digit(s) after the decimal point. For example:

int requiredShares = (int)(1230.98);

The value of requiredShares is 1230. This is not right. The correct
number should be 1231 because that's the closest integer to 1230.98.

In summary, what I want is to round up to 1 if it's greater than .5
and round down to 0 if it's less than .5.

Anyone can tell me if there's any .NET utility that does what I want?
  #2  
Old October 10th, 2008, 09:45 PM
Mark Salsbery [MVP]
Guest
 
Posts: n/a
Default Re: Isssue with converting double to int

"Curious" <fir5tsight@yahoo.comwrote in message
news:3963e61e-47c1-49fc-bbe9-5dff0d5512f2@u57g2000hsf.googlegroups.com...
Quote:
Hi,
>
I have a double number and need to convert it to an int. Unfortunately
it simply drops the digit(s) after the decimal point. For example:
>
int requiredShares = (int)(1230.98);
>
The value of requiredShares is 1230. This is not right. The correct
number should be 1231 because that's the closest integer to 1230.98.
>
In summary, what I want is to round up to 1 if it's greater than .5
and round down to 0 if it's less than .5.
>
Anyone can tell me if there's any .NET utility that does what I want?

int rounded = (int)System.Math.Round(1230.98, 0);


Mark

--
Mark Salsbery
Microsoft MVP - Visual C++

  #3  
Old October 10th, 2008, 09:45 PM
Jeroen Mostert
Guest
 
Posts: n/a
Default Re: Isssue with converting double to int

Curious wrote:
Quote:
I have a double number and need to convert it to an int. Unfortunately
it simply drops the digit(s) after the decimal point. For example:
>
int requiredShares = (int)(1230.98);
>
The value of requiredShares is 1230. This is not right. The correct
number should be 1231 because that's the closest integer to 1230.98.
>
Conversion by truncation is a time-honored tradition in C-derived languages,
and you're a blasphemer for suggesting it's "not right". Just so you know. :-)
Quote:
In summary, what I want is to round up to 1 if it's greater than .5
and round down to 0 if it's less than .5.
>
What do you want to round to when it's exactly 0.5, then?

Use Math.Round(), *then* convert the result. Math.Round() also has an
overload for specifying how to round numbers that are exactly between two
integers.

--
J.
  #4  
Old October 13th, 2008, 02:05 PM
Curious
Guest
 
Posts: n/a
Default Re: Isssue with converting double to int

Quote:
int rounded = (int)System.Math.Round(1230.98, 0);
Thanks Mark! This works!
  #5  
Old October 13th, 2008, 02:05 PM
Curious
Guest
 
Posts: n/a
Default Re: Isssue with converting double to int

Jeroen:
Quote:
What do you want to round to when it's exactly 0.5, then?
I don't have to be concerned about this situation.
Quote:
Use Math.Round(), *then* convert the result. Math.Round() also has an
overload for specifying how to round numbers that are exactly between two
integers.
Thanks for letting me know. It works!
 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles