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 - Round DOWN of a number of double type

Reply
 
Old 07-16-2009   #1 (permalink)
Curious


 
 

Round DOWN of a number of double type

I want to get the integer part of a number declared as double type.
For instance, for 8.76, I want to get 8 instead of 9. In other words,
I want to cut off anything after the decimal point. Any advice on how
to do this?

My System SpecsSystem Spec
Old 07-16-2009   #2 (permalink)
PvdG42


 
 

Re: Round DOWN of a number of double type


"Curious" <fir5tsight@xxxxxx> wrote in message
news:aaee8f7e-68bb-42b8-b175-193acf98cc17@xxxxxx
Quote:

> I want to get the integer part of a number declared as double type.
> For instance, for 8.76, I want to get 8 instead of 9. In other words,
> I want to cut off anything after the decimal point. Any advice on how
> to do this?
As you don't tell us what language, in general terms: cast it to an integer
type. That will truncate the decimal portion of the double.


My System SpecsSystem Spec
Old 07-16-2009   #3 (permalink)
Curious


 
 

Re: Round DOWN of a number of double type

On Jul 16, 10:09*am, "PvdG42" <p...@xxxxxx> wrote:
Quote:

> "Curious" <fir5tsi...@xxxxxx> wrote in message
>
> news:aaee8f7e-68bb-42b8-b175-193acf98cc17@xxxxxx
>
Quote:

> > I want to get the integer part of a number declared as double type.
> > For instance, for 8.76, I want to get 8 instead of 9. In other words,
> > I want to cut off anything after the decimal point. Any advice on how
> > to do this?
>
> As you don't tell us what language, in general terms: cast it to an integer
> type. That will truncate the decimal portion of the double.
Thanks for the suggestion! FYI, I use C#.NET.
My System SpecsSystem Spec
Old 07-16-2009   #4 (permalink)
Andrew Morton


 
 

Re: Round DOWN of a number of double type

Curious wrote:
Quote:

> I want to get the integer part of a number declared as double type.
> For instance, for 8.76, I want to get 8 instead of 9. In other words,
> I want to cut off anything after the decimal point. Any advice on how
> to do this?
What if it's negative?

Anyway, Math.Truncate does what you appear to want.

Andrew


My System SpecsSystem Spec
Old 07-16-2009   #5 (permalink)
Curious


 
 

Re: Round DOWN of a number of double type

On Jul 16, 10:48*am, "Andrew Morton" <a...@xxxxxx-press.co.uk.invalid>
wrote:
Quote:

> Curious wrote:
Quote:

> > I want to get the integer part of a number declared as double type.
> > For instance, for 8.76, I want to get 8 instead of 9. In other words,
> > I want to cut off anything after the decimal point. Any advice on how
> > to do this?
>
> What if it's negative?
>
> Anyway, Math.Truncate does what you appear to want.
>
> Andrew
I did a little experiment. a cast from double to int works fine for
both positive and negative numbers. It simply drops the residuals
after the decimal point.

However, if I use Math.Truncate, it gives me a runtime error, "Cannot
implicitly convert type 'double' to 'int'". FYI, the way I use it is
below:

double a = 8.9;
int correct1;
correct1 = Math.Truncate(a);

mLogSw.WriteLine(correct1.ToString());


double j = -4.3;
int correct2;
correct2 = Math.Truncate(j);

mLogSw.WriteLine(correct2.ToString());

double k = -8.7;
int correct3;
correct3 = Math.Truncate(k);
mLogSw.WriteLine(correct3.ToString());

My System SpecsSystem Spec
Old 07-16-2009   #6 (permalink)
Andrew Morton


 
 

Re: Round DOWN of a number of double type

"Curious" wrote
Quote:
Quote:
Quote:

>> > I want to get the integer part of a number declared as double type.
>> > For instance, for 8.76, I want to get 8 instead of 9. In other words,
>> > I want to cut off anything after the decimal point. Any advice on how
>> > to do this?
> I did a little experiment. a cast from double to int works fine for
> both positive and negative numbers. It simply drops the residuals
> after the decimal point.
>
> However, if I use Math.Truncate, it gives me a runtime error, "Cannot
> implicitly convert type 'double' to 'int'".
I suggest you use the method you've found to work.

(You didn't mention in your original post that you wanted to get the integer
part into an integer variable.)

If you check the help for the return type of the Math.Truncate method you'll
see why you got the runtime error, however; although I know nothing of C#, I
suspect the resolution to getting the result of Math.Truncate to be an Int
would make using Math.Truncate redundant.

HTH,

Andrew

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
type double quote Vista General
Writing Number to Text File: Type Problem? VB Script
why do my letters jump around when i type the letters 't', 's' or the number '0' Vista General
Can i do this, this way round ? General Discussion
vista only lets me type number in cd:key Vista Games


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