为什么int exp1 = 14/20 * 100;在java中等于'0'? [英] Why does int exp1 = 14/20*100; equals '0' in java?
本文介绍了为什么int exp1 = 14/20 * 100;在java中等于'0'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试做一些基本的数学运算,它会一直弹出 0
。我确定它与 int
有关,但我不知道如何解决它。我需要使用整数,但数学到达那些整数使用小数。我该怎么办?
I'm trying to do some basic math and it keeps popping up as 0
. I'm sure it has to do with it being an int
but I don't know how to work around it. I need to use integers but the math to arrive at those integers uses decimals. How do I do it?
推荐答案
这对黑莓来说并不特别,它是标准的java行为。
This is not special to blackberry, it's standard java behaviour.
这是因为你在做整数数学:
This is because you're doing integer math:
int subexpr1 = 14 / 20; // 0
int subexpr2 = subexpr1 * 100; // 0
使用双倍代替或更改订单
Use a double instead or change the order
int expr1 = (int) 14.0/20 * 100; // Very small possibility of rounding errors
int expr2 = 14 * 100 / 20; // Will ignore fraction parts
这篇关于为什么int exp1 = 14/20 * 100;在java中等于'0'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文