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

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

xcode - Swift Playground - Files are not readable -

jboss7.x - JBoss AS 7.3 vs 7.4 and differences -