Division on the fly [duplicate]

北慕城南 提交于 2019-12-02 03:53:01

Numbers are int by default in Java. So when you do 5/9, since they're both ints, you'll get 0.something, and since this is an int, only 0 will be stored. Solution:

double s = 5.0/9; //or 5d/9

Note that you can only explicitly cast one side, the other will be implicitly cast.

Now the calculation will be in a double manner and you'll get the desired result.

or you can do (5d/9d); // OR (5d/9) making 5 and 9 explicit double

It's good practice, when you deal with doubles, to write code like this:

double Fahrenheit = 100.0;
double celsius = (Fahrenheit - 32.0) * (5.0 / 9.0);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!