I'm starting to learn C #. I decided to write a simple console converter. Explain, please, why the following line of code does not work:

double nCels = Convert.ToDouble ( (nFahr - 32)*(5/9)); Console.WriteLine(nCels); 

Any nFahr returns 0

    1 answer 1

    Try this:

     Console.WriteLine( (double)((nFahr - 32)*(5/9)) ); 

    It is called "Explicit conversion", you can read about conversions and type conversions on MSDN

    • *(5/9) for some reason, it persistently returns 0. But /1.8 gives the correct result. - sergarcada
    • 2
      Well, so (5/9) it is all the same that int x = 5, int y = 9, (x / y) = 0. Here (5.0 / 9.0) will give the correct result. - Olter
    • Olter, thank you. So it really works. Somehow it never occurred to me that 5/9 would give an integer. At the same time clarified why 9/5 gives a unit. - sergarcada
    • 2
      because for the int operator / performs division without remainder, to get a double , as already mentioned, you need to add zero or the letter d - 5d/9d - Specter
    • So why are these little letters d, M ... - sergarcada