在java中计算百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24278647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating percentage in java
提问by user2360087
If I want to calculate 76/266 = 28.57% in java, what's the best data type to use?
如果我想在 java 中计算 76/266 = 28.57%,最好使用什么数据类型?
So far, I have:
到目前为止,我有:
int x = 76;
int y = 266;
float z = (x * 100) / y;
But doing this, I get 28.0 as an answer. I need to get an answer rounded to the nearest hundredth place. Thanks.
但是这样做,我得到 28.0 作为答案。我需要得到一个四舍五入到最近的百位的答案。谢谢。
采纳答案by paradox
Maybe you can use java.math.BigDecimalfor your calculation. I believe this is the highest precision datatype in Java
也许您可以使用java.math.BigDecimal进行计算。我相信这是 Java 中精度最高的数据类型
BigDecimal d1 = new BigDecimal(67.67);
BigDecimal d2 = new BigDecimal(67.68);
BidDecimal d3 = d1.divide(d2); // d1 + d2 is invalid
回答by griffon vulture
float x = 76;
float y = 266;
float z = x * 100 / y;
//=28.571428
If you want to round it, use:
如果你想四舍五入,请使用:
double x = 76;
double y = 266;
double z = Math.round(x * 100 / y* 100.0) / 100.0;
//=28.57
btw, as you see you don't need the parenthesis in your calculation there is a operator precedence...
顺便说一句,正如您所看到的,您的计算中不需要括号,有一个运算符优先级......
回答by Christian
In Java and some other programming languages, there is something called integer arithmetic, which says that if you do (in your case):
在 Java 和其他一些编程语言中,有一种叫做integer algorithm 的东西,它说如果你这样做(在你的情况下):
int / int = int
In your code, you are doing
在你的代码中,你正在做
(int * int) / int <=> int / int = int
Solutions:
解决方案:
Method 1:Something you can do to get a float is to use a float
operand. In your case it can be the 100
:
方法 1:获得浮点数的方法是使用float
操作数。在您的情况下,它可以是100
:
float z = (x * 100.0f) / y;
Here, the operation is
这里的操作是
(int * float) / int <=> float / int = float
Method 2:Another way to solve this is to castan integer to a float:
方法2:解决这个另一种方法是铸造一个浮动的整数:
float z = (x * 100) / (float)y; // int * int / float = float
float z = (float)x * 100 / y; // float * int / int = float
Method 3:As @webSpider mentioned in his answer, you can just declare the variables x
and y
as float
to avoid these problems.
方法3:当@webSpider在他的回答中提到,你可以声明变量x
,并y
为float
避免这些问题。
Edit:To round your float
result, you can try this:
编辑:要舍入你的float
结果,你可以试试这个:
float z = Math.round(result * 100) / 100f;
where the number of zeros of 100
is the number of decimal places. Note that 100f
will be a float
because of the postfix f
.
其中零100
的个数是小数位数。请注意,这100f
将是float
因为 postfix f
。
回答by gprathour
What you are doing in your code is,
你在你的代码中所做的是,
multiply an integer x to 100 and then divide the result by integer y. So their output will be integer only.
将整数 x 乘以 100,然后将结果除以整数 y。所以他们的输出只会是整数。
Then you are storing this int result in a float variable. So it just adds .0
to your result.
然后您将这个 int 结果存储在一个浮点变量中。所以它只会增加.0
你的结果。
To get the result you want you can do any of the following,
要获得您想要的结果,您可以执行以下任何操作,
1. int x = 76;
int y = 266;
float z = (x * 100.0f) / y;
Note do not write 100.0 because it will be treated as a double number so you will get loss of precision error
Note do not write 100.0 because it will be treated as a double number so you will get loss of precision error
2. float x = 76;
int y = 266;
float z = (x * 100) / y;
3. float x = 76;
float y = 266;
float z = (x * 100) / y;