Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Division in c# gives wrong value.

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 08-03-2010
Member
 
Join Date: Nov 2009
Posts: 37
Division in c# gives wrong value.

Hello friends,
I am tried to write code to show my image correctly. In one of program I have two numbers like "breedtesPlaatjes" and "hoogtesPlaatjes" with value 900 and 600 respectively. When I load these two numbers I get wrong result. I don't know why Division in c# gives wrong value. Please help me. I have written following code for your information.
Code:
{
    double verHs = (hoogtePlaatjes/breedtePlaatjes);
    int vHeights = Converts.ToInt32(verHs * 239);

    msOptsMediums.Height = vHeight;
    msOptsMediums.Width = 239;

    
    if (hoogtePlaatje < 179)
    {
        mOptsMediums.Height = 179;
        mOptsMediums.Width = 239;
    }
}
Reply With Quote
  #2  
Old 08-03-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
Re: Division in c# gives wrong value.

You must know that when you divide int value by another int value it gives int result.
Code:
double verHs = (hoogtePlaatjes/breedtePlaatjes);
In your code your "verHs" is double and you are tried to assign them int value and that's why you are getting such type of problem. In this case change the data type of breedtePlaatjes and hoogtePlaatjes to double to get proper result.
Reply With Quote
  #3  
Old 08-03-2010
Member
 
Join Date: May 2008
Posts: 1,990
Re: Division in c# gives wrong value.

You have to use one of the parameters of the division to be of float type, so that you can get result of float type. This is very easy to do. You can do this by casting one of them to a float. Just look at following code.
Code:
double verHss = (double)hoogtePlaatjess/breedtePlaatjess;
Or you can use
Code:
double verHs = hoogtePlaatjes/(double)breedtePlaatjes;
Reply With Quote
  #4  
Old 08-03-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
Re: Division in c# gives wrong value.

From your information it seems that you haven't learn C# language very and that's why you have made this silly mistake. You are tried to assign inst value to variable of data type double. In this case you have to cast one of the variables to double, like this:
Code:
double verHs = (double)hoogtePlaatjes/breedtePlaatjes;
Reply With Quote
  #5  
Old 08-03-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
Re: Division in c# gives wrong value.

Code:
double verHs = (hoogtePlaatjes/breedtePlaatjes);
In your code the hoogtePlaatjess and breedtePlaatjess are of int type and you are tried to assign them to variable of double data type and that's why you are getting such type of problem. In this case you have to use following:
Code:
double verHs = double (hoogtePlaatjes) / breedtePlaatejes;
In above code I have cast hoogtePlaatjes to double.
Reply With Quote
  #6  
Old 08-03-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
Re: Division in c# gives wrong value.

As per my information when you are tried to divide two integers and if your division generate fractional part, then that fractional part is discarded. In your case you're getting:
900 / 350 = 0 + 3.5/9
Which, discarding the fractional part, gives:
900 / 350 = 0
In this case to get floating point result, you have to cast one of the arguments to double to fix this problem.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Division in c# gives wrong value."
Thread Thread Starter Forum Replies Last Post
What is Code Division Multiple Access NetWorkInG Networking & Security 1 05-10-2010 09:39 PM
How to hide the 100MB division? Heather5 Operating Systems 4 10-09-2010 09:47 AM
Deleting boot-camp Win 7 division Sauk Operating Systems 6 08-09-2010 09:14 AM
Subnet mask calculation and division Osman84 Networking & Security 5 18-03-2010 02:14 AM
Result of division in java Aaliya Seth Software Development 5 03-03-2010 11:34 AM


All times are GMT +5.5. The time now is 11:12 AM.