不懂就问,为什么 double 后面一定要加上一个 0?

主使用了多年,也一直没搞明白为什么要加 0,再网上搜了一下,也讲的不清不楚的,有没有大神简单通俗的说一下?
为什么设计的时候,不直接给转化成 double 呢?

主使用了多年,也一直没搞明白为什么要加 0,再网上搜了一下,也讲的不清不楚的,有没有大神简单通俗的说一下?
为什么设计的时候,不直接给转化成 double 呢?
你也不说报啥错。渣男! > If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6).
> Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.
所以显示声明 是 long/(float|double),换句话说等价于 double period = (end – start)/(double) 1000000;
参见 https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.2
这种问题,Google 搜一下因为英文关键词, “java division of integers” 前面几条都是有效答案。。
当然,也可以不在后面加 .o,可以在前面 (double)。
至于为什么不直接整数相除就得到浮点数,因为那样设计的话,那取整的时候会麻烦些,麻烦不会消失只会转移,而且我估计会有更多人问和你相反的问题。
对比一下两者的结果。没有 .0 的是整形,除了还是整形。在你的例子就是除了还是 long 型。
写一个 100, 结果程序自动转成 double 了, 你感觉合理吗?
说实话,我最恶心的事情之一,就是同事碰到问题了,连错误信息都不看一眼就在那里嚷嚷“错了,过来帮我看一下”,等你过去一看,哦,file not found:/var/www/site/config/app.conf,tmd 连路径都给你指出来了还在那里瞎 jb 嚷嚷“好奇怪啊昨天还能用的”。
然后同事就喊,后台崩了,后台崩了,快修复,根本不看英文