|
| |||||||||
| Tags: c sharp language, class, file handle, function, main, object |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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;
}
} |
|
#2
| ||||
| ||||
| 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); |
|
#3
| |||
| |||
| 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; Code: double verHs = hoogtePlaatjes/(double)breedtePlaatjes; |
|
#4
| ||||
| ||||
| 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; |
|
#5
| ||||
| ||||
| Re: Division in c# gives wrong value. Code: double verHs = (hoogtePlaatjes/breedtePlaatjes); Code: double verHs = double (hoogtePlaatjes) / breedtePlaatejes; |
|
#6
| ||||
| ||||
| 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. |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |