本文介绍了在Java中使用双精度类型进行除法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的。我一直在脑海里猛击了大约2个小时,试图弄清为什么世界上 double answer = 364/365; 告诉我 answer 为0。或者 double 的任何其他组合,它只是截断了小数,我只是不知道为什么。
Okay. I have been bashing my head against the wall for like 2 hours now trying to figure out why in the world double answer = 364/365; is telling me that answer is 0. Or any other combination of double for that matter, its just truncating the decimal and I just don't know why.
推荐答案364/365执行整数除法(截断小数点)。
364/365 performs integer division (truncates the decimal).
尝试双重答案= 364.0 / 365; 强制其执行浮点除法。
Try double answer = 364.0/365; to force it to perform floating point division.
类似于:
double days_in_year = 365; double answer = 364/days_in_year;也可以工作,因为其中一个操作数不是整数。
would work as well, since one of the operands isn't an integer.
更多推荐
在Java中使用双精度类型进行除法时出错
发布评论