Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 19th, 2010, 03:15 AM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default 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.
  #2 (permalink)  
Old May 19th, 2010, 03:28 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #3 (permalink)  
Old May 19th, 2010, 03:02 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default

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.
  #4 (permalink)  
Old May 19th, 2010, 03:38 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default

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
  #5 (permalink)  
Old May 19th, 2010, 03:49 PM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Code:
            var pc = 10 / 3;
            Console.WriteLine(pc.GetType());
  #6 (permalink)  
Old May 19th, 2010, 04:29 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default

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.
  #7 (permalink)  
Old May 19th, 2010, 06:49 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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.
================================================== =========
  #8 (permalink)  
Old May 19th, 2010, 08:17 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thanks. i got your point. but what do you mean by:

"Apart form that GOOGLE is your best friend"
  #9 (permalink)  
Old May 19th, 2010, 09:46 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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.
================================================== =========
  #10 (permalink)  
Old May 20th, 2010, 02:59 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you divide two integers then integer division (which always produces an integer) will always be performed. The enforce none-integer 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
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 


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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue with replace function using regex avator BOOK: Beginning Regular Expressions 0 May 26th, 2008 03:46 AM
Date function issue Grafixx01 Access 2 April 11th, 2007 02:32 PM
Math question dparsons ASP.NET 1.0 and 1.1 Professional 5 June 14th, 2006 02:31 PM
Math on a Form kelcontech Classic ASP Basics 4 May 12th, 2004 05:01 PM



All times are GMT -4. The time now is 04:05 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.