Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Isssue with converting double to int

Reply
 
Old 10-10-2008   #1 (permalink)
Curious


 
 

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?

My System SpecsSystem Spec
Old 10-10-2008   #2 (permalink)
Mark Salsbery [MVP]


 
 

Re: Isssue with converting double to int

"Curious" <fir5tsight@xxxxxx> wrote in message
news:3963e61e-47c1-49fc-bbe9-5dff0d5512f2@xxxxxx
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++

My System SpecsSystem Spec
Old 10-13-2008   #3 (permalink)
Curious


 
 

Re: Isssue with converting double to int

Quote:

> int rounded = (int)System.Math.Round(1230.98, 0);
Thanks Mark! This works!
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Converting FROM .wma and .wav TO .mp3 using WMC 11 Vista music pictures video
Converting MP4 to MP3 Vista music pictures video
Converting a .Wim To a ISO Vista General
copy to c:\users\default - user shell folder autocad 2008 isssue Vista General
Converting to PDF Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46