Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在简单的计算中使用 ceil Math.ceil。但是,Math.ceil(3/2)出1.0,Math.ceil(1.5)而出2.0,这是为什么呢?我应该自己纠正这个以使上限操作3/2成为 2.0 吗?
Math.ceil
Math.ceil(3/2)
Math.ceil(1.5)
3/2
第一种情况等于Math.ceil(1)因为3/2是整数除法。
Math.ceil(1)
3/2 将返回 int 所以我不会携带浮点值。但是(1.5)是浮点型。 Java 中的 3/2=1 所以 Math.ceil(1)=1.0 所以 Math.ceil(3/2) 将返回 1.0。和 Math.ceil(1.5) 返回 2.0