java - Unexpected output in dividing two numbers -
this question has answer here:
why code giving 0.0
answer?
public static void main(string[] args) { float ans = (480/1080); system.out.println(ans); }
you dividing 2 integers, result integer. 480/1000 < 1 , therefore truncated 0. result cast float stored in float variable.
to divide numbers floats, cast 1 of them:
float ans = ((float)480/1080);
Comments
Post a Comment