-4

I can't get float value from int/int.

float rs;
int a=10;
int b=3;
rs=(float)a/b;

Result : 3.0 . I need 3.333 Thank!

cs95
  • 330,695
  • 80
  • 606
  • 657

2 Answers2

1

Testing your code, it does indeed give 3.333, because the typecast takes precedence... did you execute some other code?


Another possible option is to typecast b.

rs = a / (float)b;

You could also typecast a, but you'd need an extra set of parenthesis.

Here's an ideone demo.

cs95
  • 330,695
  • 80
  • 606
  • 657
-1

EDIT: The cast has priority:

rs = (float) a / b;

Or,

float new_a = a;
rs = new_a / b;
fileyfood500
  • 1,023
  • 11
  • 24