HighTechTalks DotNet Forums  

How to check whether the value is a whole number not?

Dotnet Academic General Discussions microsoft.public.dotnet.academic


Discuss How to check whether the value is a whole number not? in the Dotnet Academic General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Siew Yee
 
Posts: n/a

Default How to check whether the value is a whole number not? - 08-12-2005 , 05:35 AM






Hi,

Does C# provide a method to check/validate whether the value is a whole
number not?
Thanks.

Regards,
Siew Yee

Reply With Quote
  #2  
Old   
Peter van der Goes
 
Posts: n/a

Default Re: How to check whether the value is a whole number not? - 08-12-2005 , 09:06 AM







"Siew Yee" <SiewYee (AT) discussions (DOT) microsoft.com> wrote

Quote:
Hi,

Does C# provide a method to check/validate whether the value is a whole
number not?
Thanks.

Regards,
Siew Yee
Reaseach the .NET Framework Convert class. If, for example, you were to use
the Convert.ToInt32 method in a try - catch block, any attempt to convert an
argument that is not an integer will throw an exception and you can deal
with it.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.




Reply With Quote
  #3  
Old   
Siew Yee
 
Posts: n/a

Default Re: How to check whether the value is a whole number not? - 08-13-2005 , 03:01 AM



Thanks. I will go try it out.

"Peter van der Goes" wrote:

Quote:
"Siew Yee" <SiewYee (AT) discussions (DOT) microsoft.com> wrote in message
news:E2563ABC-B3F6-4C42-8586-F00E48627B10 (AT) microsoft (DOT) com...
Hi,

Does C# provide a method to check/validate whether the value is a whole
number not?
Thanks.

Regards,
Siew Yee

Reaseach the .NET Framework Convert class. If, for example, you were to use
the Convert.ToInt32 method in a try - catch block, any attempt to convert an
argument that is not an integer will throw an exception and you can deal
with it.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.




Reply With Quote
  #4  
Old   
Scott
 
Posts: n/a

Default Re: How to check whether the value is a whole number not? - 10-01-2005 , 02:43 PM



(see inline)

"Peter van der Goes" <p_vandergoes (AT) toadstool (DOT) u> wrote

Quote:
"Siew Yee" <SiewYee (AT) discussions (DOT) microsoft.com> wrote in message
news:E2563ABC-B3F6-4C42-8586-F00E48627B10 (AT) microsoft (DOT) com...
Hi,

Does C# provide a method to check/validate whether the value is a whole
number not?
Thanks.

Regards,
Siew Yee

Reaseach the .NET Framework Convert class. If, for example, you were to
use the Convert.ToInt32 method in a try - catch block, any attempt to
convert an argument that is not an integer will throw an exception and you
can deal with it.
I have the source code (from MS "Shared Source", though i can't remember
actually where I found it) for a number of the framework classes, including
Convert. Converting from a "double" to an Int32 is allowed and does not
throw exceptions (except out of bounds). Since the shared source is public,
here's the code excerpt:

public static int ToInt32(double value) {
if (value >= 0) {
if (value < 2147483647.5) {
int result = (int)value;
double dif = value - result;
if (dif > 0.5 || dif == 0.5 && (result & 1) != 0) result++;
return result;
}
}
else {
if (value >= -2147483648.5) {
int result = (int)value;
double dif = value - result;
if (dif < -0.5 || dif == -0.5 && (result & 1) != 0) result--;
return result;
}
}
throw new
OverflowException(Environment.GetResourceString("O verflow_Int32"));
}

Scott




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.