removing decimal portion of a real number

  • Thread starter Thread starter Homer Simpson
  • Start date Start date
H

Homer Simpson

I'm using VS2005 Beta 2 (C#) and would like to know how I can remove the
decimal portion (to the right of the decimal) of a real number. I want the
whole number portion but do not wish to discard the decimal portion; that
will be used for additional calcs.

Does C# have any methods for doing this?

Thanks,
Scott
 
Create a temp int and cast the real number to it, or use the cast in an expression.

using System;

class CastToInt {

public static int Main(string[] args) {

double pi = 3.14159265358979;

int nPi = (int)pi;

Console.WriteLine("pi = {0} and pi cast to int = {1}", pi, nPi);

Console.WriteLine("pi = {0} and pi cast to int = {1}", pi, (int)pi);

Console.Write("\nPress Enter to continue...");

Console.Read();

return 0;

}

}
 
Create a temp int and cast the real number to it, or use the cast in an
expression.
Agreed.

Another option is the Math.floor() function.

Greetings,
Wessel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top