0

Possible Duplicate:
Division returns zero

Say you have the code below.

double d=16/60; //I got d is 0.0 , but I expected d could be 0.27. 

How to make it ?thanks.

Edit for the Best Answer :)

double d=16/60f;
d= Math.Round(d, 2); //result being 0.27
Community
  • 1
  • 1
Joe.wang
  • 11,537
  • 25
  • 103
  • 180
  • 1
    http://stackoverflow.com/questions/5242436/c-divide-an-int-by-100 and http://stackoverflow.com/questions/2400799/division-in-c-sharp-not-going-the-way-i-expect – Habib Dec 11 '12 at 11:09
  • 1
    How is that the "Best Answer"? The resulting value of `d` is `0`. – Allon Guralnek Dec 11 '12 at 11:57

2 Answers2

7

This is due to integer division.

At least one of the operands needs to be a floating point type (float or double).

double d=16/60f;

double d=16f/60;

double d=16/60d;

double d=16d/60;

double d=16.0/60;

double d=16/60.0;
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    Note: The suffix of `f` specifies a `float` literal value that could lead to more prominent rounding errors, especially when implicitly casting it to double later. Either the suffix `d` should be sued to denote a `double` literal value, or a decimal floating point should be used (like in the last two example in the answer), which in C# creates a `double`. For example, `double d = 0.123f;` would produce a value of `0.123000003397465`. Unless you have specific performance or memory requirements, it's best to avoid `float` altogether. – Allon Guralnek Dec 11 '12 at 11:51
1

Make a cast

double d = (double)16 / (double)60

Or:

double d = Convert.ToDecimal(16)/Convert.ToDecimal(60)
Link
  • 1,307
  • 1
  • 11
  • 23