Java 如何使 2 个整数的除法产生一个浮点数而不是另一个整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/787700/
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 to make the division of 2 ints produce a float instead of another int?
提问by phill
In another Bruce Eckels exercise in calculating velocity, v = s / t
where s and t are integers. How do I make it so the division cranks out a float?
在另一个 Bruce Eckels 练习计算速度,v = s / t
其中 s 和 t 是整数。我如何做到这一点,以便该部门产生浮动?
class CalcV {
float v;
float calcV(int s, int t) {
v = s / t;
return v;
} //end calcV
}
public class PassObject {
public static void main (String[] args ) {
int distance;
distance = 4;
int t;
t = 3;
float outV;
CalcV v = new CalcV();
outV = v.calcV(distance, t);
System.out.println("velocity : " + outV);
} //end main
}//end class
采纳答案by Alnitak
Just cast one of the two operands to a float first.
只需先将两个操作数之一转换为浮点数。
v = (float)s / t;
The cast has higher precedence than the division, so happens before the division.
演员表的优先级高于除法,所以发生在除法之前。
The other operand will be effectively automatically cast to a float by the compiler because the rules say that if either operand is of floating point type then the operation will be a floating point operation, even if the other operand is integral. Java Language Specification, §4.2.4and §15.17
另一个操作数将被编译器有效地自动转换为浮点数,因为规则说如果任一操作数是浮点类型,那么该操作将是一个浮点操作,即使另一个操作数是整数。 Java 语言规范,§4.2.4和§15.17
回答by Jhonny D. Cano -Leftware-
You can cast the numerator or the denominator to float...
您可以将分子或分母转换为浮动...
int operations usually return int, so you have to change one of the operanding numbers.
int 操作通常返回 int,因此您必须更改操作数之一。
回答by anisoptera
Try:
尝试:
v = (float)s / (float)t;
Casting the ints to floats will allow floating-point division to take place.
将整数转换为浮点数将允许进行浮点除法。
You really only need to cast one, though.
不过,你真的只需要投射一个。
回答by Jason Coco
Cast one of the integers to a float to force the operation to be done with floating point math. Otherwise integer math is always preferred. So:
将整数之一转换为浮点数以强制使用浮点数学完成运算。否则整数数学总是首选。所以:
v = (float)s / t;
回答by Uri
You can cast even just one of them, but for consistency you may want to explicitly cast both so something like v = (float)s / (float)t should work.
您甚至可以只转换其中一个,但为了保持一致性,您可能希望显式转换两者,以便像 v = (float)s / (float)t 这样的东西应该工作。
回答by space programmer
To lessen the impact on code readabilty, I'd suggest:
为了减少对代码可读性的影响,我建议:
v = 1d* s/t;
回答by Nikhil Kumar
Cast one of the integers/both of the integer to float to force the operation to be done with floating point Math. Otherwise integer Math is always preferred. So:
将整数之一/两个整数转换为浮点数以强制使用浮点数学完成运算。否则整数数学总是首选。所以:
1. v = (float)s / t;
2. v = (float)s / (float)t;
回答by May I help you
Try this:
尝试这个:
class CalcV
{
float v;
float calcV(int s, int t)
{
float value1=s;
float value2=t;
v = value1 / value2;
return v;
} //end calcV
}