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) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -