Tigraine
Daniel Hoelbling talks about .NET

Divide two integers in .NET

March 26th, 2009 . by Daniel Hölbling

Ok, this is really simple, but it just has cost me 15 minutes because I ignored a compiler warning:

double result = myInt1/myInt2;

The compiler will underline that there is a possible loss in fraction, but I ignored it while not seeing anything wrong with the code at all.

Whatever values I calculated, the result was always an integer. Turns out, dividing two integers will also create an integer instead of a double (so I got weird results like 31/10 = 3 ).

To bypass this, you need to promote at least one of your integers to a double so a double divide will occur:

double result = (double)myInt1 / myInt2;

Hope this helps, took me far too long to figure out.


View Comments to “Divide two integers in .NET”

  1. comment number 1 by: stefan

    tz… Anfänger-Fehler ;p

  2. comment number 2 by: air jordan 13

    Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
    Gucci sweaters
    If its common it might be difficult to get, however, be creative and you can usually find something.

  3. comment number 3 by: gucci handbags

    Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
    Jordan OL School
    If its common it might be difficult to get, however, be creative and you can usually find something.

Leave a Reply

Name

Mail (never published)

Website

blog comments powered by Disqus