
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum. 
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com



May 19th, 2010, 03:15 AM

Authorized User


Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts


Issue in Math function in c#.
I have two issues:(Questions may be silly,sorry about that)
1. I want to check whether the variable containing value is decimal? is there a function to check its datatype?
eg: int pc=10/3; //how can i check whether pc is decimal or integer?
2. I want to Round the decimal value to the next integer value. What is the function to do that.
eg: decimal p=4.3; //the result should be 5.
Thanks for your time.

May 19th, 2010, 03:28 AM


Friend of Wrox


Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts


The answer to question two depends on how you want the rounding to work  you are probably wanting either the Math.Round or the Math.Floor methods.
The answer to question one is it will always be an integer, because you've declared it as an integer.

May 19th, 2010, 03:02 PM

Authorized User


Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts


Thanks for the answer two. But i tried Math.round and Math.floor. It just rounds to the nearest number but not the next integer.
For question one, i think i missed some information. i am actually dividing two values and wants to check if the result is integer or decimal. So how will i check it?
int Totrec1 = count1;//I calculated count1 which is also int.
int pagesize1 = 3;
int pcount;//i always want pcount to be integer.
int pagecount1 = Totrec1 / pagesize1;
// i am not getting the result as exact value,instead iam always getting left most integer value ,excluding the decimal value even if i change the datatype of pagecount1 to Float.
eg: if totrec1=7,pagesize1=3 ie 7/3=2.3 //i want the result as 2.3, and not just 2.Here i want to check whether 2.3 is decimal or integer and if decimal then i want to round it to next integer ie 3. Thats what i want.
pcount = Convert.ToInt32(Math.Round(pagecount1));//Again here i am getting only 2 coz already the result of 7/3 i got is 2 not 2.3.
Looks very simple logic, but i cant execute it.

May 19th, 2010, 03:38 PM

Authorized User


Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts


i got the result for question one. all datatypes has to be in Float or double to get the exact decimal value.
I just want to know how to check its datatype whether its of float or integer.
thanks

May 19th, 2010, 03:49 PM

Friend of Wrox


Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts


Code:
var pc = 10 / 3;
Console.WriteLine(pc.GetType());

May 19th, 2010, 04:29 PM

Authorized User


Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts


GetType() is used to get the object's datatype, but how to check its datatype
for eg: i just want to check like:
float f=7;
float b=3;
float res=7/3;//thr res will be 2.3, if res=9/3 it returns integer val 3.
so i want to check if res contains decimal values or intger.

May 19th, 2010, 06:49 PM

Friend of Wrox


Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts


Apart form that GOOGLE is your best friend (didn't you even try this??? 'decimal part .net'), to get the decimal part of the number just do:
DECIMAL = NUMBER  Math.Round(NUMBER).
if decimal is not 0, then you have a result.
Also look for FIX to get the nearest integer of a value (not the rounded).
__________________
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the proof.
================================================== =========

May 19th, 2010, 08:17 PM

Authorized User


Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts


Thanks. i got your point. but what do you mean by:
"Apart form that GOOGLE is your best friend"

May 19th, 2010, 09:46 PM

Friend of Wrox


Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts


because googling this: decimal part .net, I get a lot of results, even the one I already know (the one I point out).
Sometimes when you need something, better ask google first...
__________________
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the proof.
================================================== =========

May 20th, 2010, 02:59 AM


Friend of Wrox


Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts


If you divide two integers then integer division (which always produces an integer) will always be performed. The enforce noneinteger division either convert them to floats first, or multiply by a float to start.
int a = 1;
int b = 2;
float c = a/b; // c equals 1.0
float d = 1.0 * a / b; // d equals 0.5
You keep asking how to check the 'datatype' of a variable. I think you are using this word incorrectly. In the above code a and b have a data type of 'integer' and c and d have a datatype of 'float'  this does not change depending on what value they contain.
If you want to know if a float contains a number with a zero decimal then try comparing it to its rounded value.
c == Math.Floor(c) // equals true
d == Math.Floor(d) // equals false
If you want to round UP, then use Math.Ceiling instead (the exact opposite of Math.Floor)
float e = Math.Ceiling(4.3); // equals 5.0


