Search code examples
c#doublescale

Division in c# not going the way I expect


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.


Solution

  • 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.