在Java中使用双精度类型进行除法时出错

编程入门 行业动态 更新时间:2024-10-26 10:38:45
本文介绍了在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中使用双精度类型进行除法时出错

本文发布于:2023-10-28 13:41:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:除法   精度   类型   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!