0

I Just want to print full result of following equation:

float rate = (interestRate / 1200);

double rate = (interestRate / 1200);

where interestRate is 5. My problem is it always showing same value 0.0 instead 0.00416666666667 not matter which datatype I use from double or float.

How can I get full result of this simple equation? please help.

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Pari
  • 1,705
  • 4
  • 18
  • 19

4 Answers4

3

You need to cast (at least) one of the divisors:

double rate = ((double) interestRate / 1200);
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
1

This is called Integer division.

You can either cast:

double rate = ((double)interestRate / 1200);

or use:

double rate = (interestRate / 1200.0);
Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
0

your issue is related to the fact that you are performing a division between integer. You have to cast at least one of the two operands

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

You need to cast:

float rate = 0.0f;
float rate = ((float) interestRate / 1200);
Amresh
  • 2,098
  • 16
  • 20