Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Division in c# not going the way I expect

Tags:

c#

double

scale

Im trying to write something to get my images to show correctly. I have 2 numbers "breedtePlaatje" and "hoogtePlaatje". When i load those 2 vars with the values i get back "800" and "500" i expect "verH" to be (500 / 800) = 0,625. Tho the value of verH = 0..

This is the code:

int breedtePlaatje = Convert.ToInt32(imagefield.Width);
int hoogtePlaatje = Convert.ToInt32(imagefield.Height);

//Uitgaan van breedte plaatje
if (breedtePlaatje > hoogtePlaatje)
{
    double verH = (hoogtePlaatje/breedtePlaatje);
    int vHeight = Convert.ToInt32(verH * 239);

    mOptsMedium.Height = vHeight;
    mOptsMedium.Width = 239;

    //Hij wordt te klein en je krijgt randen te zien, dus plaatje zelf instellen
    if (hoogtePlaatje < 179)
    {
        mOptsMedium.Height = 179;
        mOptsMedium.Width = 239;
    }
}

Any tips regarding my approach would be lovely aswell.

like image 491
Younes Avatar asked Mar 08 '10 11:03

Younes


People also ask

How do you divide in C?

printf("Enter dividend: "); scanf("%d", &dividend); printf("Enter divisor: "); scanf("%d", &divisor); Then the quotient is evaluated using / (the division operator), and stored in quotient . quotient = dividend / divisor; Similarly, the remainder is evaluated using % (the modulo operator) and stored in remainder .

What is float division in C?

The variables b, c, d are of float type. But the / operator sees two integers it has to divide and hence returns an integer in the result which gets implicitly converted to a float by the addition of a decimal point. If you want float divisions, try making the two operands to the / floats.


1 Answers

Dividing int by int gives an int.

double verH = (hoogtePlaatje/breedtePlaatje);

The right hand side of the assignment is an integer value.

Change breedtePlaatje and/or hoogtePlaatje to double and you will get the answer you expect.

like image 108
Arve Avatar answered Sep 17 '22 19:09

Arve