I need to calculate the result using a certain formula, but this fails because it uses the division of the number 25 by the number 100.
It all looks something like this:

double i = 25 / 100; // В этом случае будет выводиться 0, а мне нужно 0.25 

What is the problem here?

    3 answers 3

    You have an int operand 25 in action divided by an int operand 100 . Accordingly, the division occurs integer. For the result you expect, at least one of the operands must be cast to double . For example:

     double i = 25 / 100d; 

      because it is an int.

       double i = 25.0 / 100; 

      or cast (cast) to double, probably it is done like this:

       double i = (double)25 / 100; 

        Change your code to:

         double i = 25 / 100d; 

        Otherwise, the compiler assumes that integer division is performed, which in this case returns 0 instead of 0.25.