如何在Java中将整数转换为浮点数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4377842/
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
How can I convert integer into float in Java?
提问by Roman
I have two integers x
and y
. I need to calculate x/y
and as outcome I would like to get float. For example as an outcome of 3/2
I would like to have 1.5. I thought that easiest (or the only) way to do it is to convert x
and y
into float type. Unfortunately, I cannot find an easy way to do it. Could you please help me with that?
我有两个整数x
和y
。我需要计算x/y
,作为结果,我想获得浮动。例如作为结果3/2
我想有 1.5。我认为最简单(或唯一)的方法是转换x
并转换y
为浮点类型。不幸的是,我找不到一种简单的方法来做到这一点。你能帮我解决这个问题吗?
采纳答案by Matt Ball
You just need to castat least one of the operands to a float:
你只需要投操作数为浮点的至少一个:
float z = (float) x / y;
or
或者
float z = x / (float) y;
or (unnecessary)
或(不必要的)
float z = (float) x / (float) y;
回答by LaGrandMere
Here is how you can do it :
您可以这样做:
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 3;
int y = 2;
Float fX = new Float(x);
float res = fX.floatValue()/y;
System.out.println("res = "+res);
}
See you !
再见 !
回答by Peter Lawrey
You shouldn't use float unless you have to. In 99% of cases, double is a better choice.
除非必须,否则不应使用浮动。在 99% 的情况下,double 是更好的选择。
int x = 1111111111;
int y = 10000;
float f = (float) x / y;
double d = (double) x / y;
System.out.println("f= "+f);
System.out.println("d= "+d);
prints
印刷
f= 111111.12
d= 111111.1111
Following @Matt's comment.
遵循@Matt 的评论。
float has very little precision (6-7 digits) and shows significant rounding error fairly easily. double has another 9 digits of accuracy. The cost of using double instead of float is notional in 99% of cases however the cost of a subtle bug due to rounding error is much higher. For this reason, many developers recommend not using floating point at all and strongly recommend BigDecimal.
float 的精度非常低(6-7 位)并且很容易显示显着的舍入误差。double 还有另外 9 位的准确度。在 99% 的情况下,使用 double 代替 float 的成本是名义上的,但是由于舍入误差导致的细微错误的成本要高得多。出于这个原因,许多开发人员建议根本不使用浮点数,并强烈建议使用 BigDecimal。
However I find that double can be used in most cases provided sensible rounding is used.
但是我发现在大多数情况下可以使用 double ,前提是使用了合理的舍入。
In this case, int x has 32-bit precision whereas float has a 24-bit precision, even dividing by 1 could have a rounding error. double on the other hand has 53-bit of precision which is more than enough to get a reasonably accurate result.
在这种情况下,int x 具有 32 位精度而 float 具有 24 位精度,即使除以 1 也可能存在舍入误差。另一方面,double 具有 53 位精度,足以获得相当准确的结果。
回答by LostOblivion
Sameer:
萨米尔:
float l = new Float(x/y)
will not work, as it will compute integer division of x and y first, then construct a float from it.
将不起作用,因为它将首先计算 x 和 y 的整数除法,然后从中构造一个浮点数。
float result = (float) x / (float) y;
Is semantically the best candidate.
在语义上是最好的候选者。
回答by user unknown
You just need to transfer the first value to float, before it gets involved in further computations:
您只需要将第一个值转移到浮点数,然后再参与进一步的计算:
float z = x * 1.0 / y;
回答by tej shah
// The integer I want to convert
// 我要转换的整数
int myInt = 100;
// Casting of integer to float
// 将整数转换为浮点数
float newFloat = (float) myInt