整数或双精度返回值

发布于 2024-11-04 01:53:56 字数 303 浏览 0 评论 0原文

我传入了一个 Integer 值,然后将其除以 100,因此结果可以是 int 或 double,所以不确定是否强制转换它。

public void setWavelength(Integer value) {
    this.wavelength = value;
}  

然后值除以 100

pluggable.setWavelength(entry.getPluggableInvWavelength()/100);

所以不知道如何转换这个值/对象

I have an Integer value been passed in and then it is divided by 100, so result could either be an int or double so not sure if cast it or not.

public void setWavelength(Integer value) {
    this.wavelength = value;
}  

then value divided by 100

pluggable.setWavelength(entry.getPluggableInvWavelength()/100);

So not sure how to cast this value/object

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ま昔日黯然 2024-11-11 01:53:56

如果将一个整数 (int) 除以另一个整数 (int),结果将再次成为整数 (int)。
-- 更多详细信息:15.17 乘法运算符

需要将其中一个或两个标记为 double

//cast the divisor entry.getPluggableInvWavelength() to double
pluggable.setWavelength( ((double) entry.getPluggableInvWavelength()) /100);

//make the constant quotient a double
pluggable.setWavelength(entry.getPluggableInvWavelength() /100.0);

请注意,java.lang.Integer 是一种不可变的包装类型,而不是 int! - 事实上你不能使用java.lang.Integer进行计算,但从Java 1.5开始编译器会自动将int转换为Integer并返回(自动装箱和自动拆箱)。但一般来说,最好理解其中的差异,并且仅当您确实需要对象(而不是要计算的数字)时才使用 Integer。

If you divide an integer (int) by an other integer (int) the result will be an integer (int) again.
-- More details: 15.17 Multiplicative Operators

You need to mark one or both as double

//cast the divisor entry.getPluggableInvWavelength() to double
pluggable.setWavelength( ((double) entry.getPluggableInvWavelength()) /100);

or

//make the constant quotient a double
pluggable.setWavelength(entry.getPluggableInvWavelength() /100.0);

Pay attention to the fact, that java.lang.Integer is a immutable wrapper type and not an int! - In fact you can not calculate with java.lang.Integer, but since Java 1.5 the compiler will convert int to Integer and back automatically (auto boxing and auto unboxing). But in general it is better to understand the difference and use Integer only if you real need objects (and not numbers to calculate).

内心旳酸楚 2024-11-11 01:53:56

如果 waveLengthdouble,则有:

entry.getPluggableWavelength() / 100d;

d 表示该数字被视为 double,因此除法结果是

If waveLength is double, then have:

entry.getPluggableWavelength() / 100d;

d means that the number is treated as double, and hence the division result is double.

分分钟 2024-11-11 01:53:56

如果将 int 除以 int,则始终会得到 int。如果您想要一个 floatdouble (因为您需要表示结果的小数部分),那么您需要转换一个或两个输入:

int a = 3;
int b = 4;

int    c1 = a / b;                  // Equals 0
double c2 = a / b;                  // Still equals 0
double c3 = (double)a / (double)b;  // Equals 0.75

If you divide an int by an int, you always get an int. If you want a float or a double (because you need to represent fractional parts of the result), then you'll need to cast one or both inputs:

int a = 3;
int b = 4;

int    c1 = a / b;                  // Equals 0
double c2 = a / b;                  // Still equals 0
double c3 = (double)a / (double)b;  // Equals 0.75
じее 2024-11-11 01:53:56

如果entry.getPluggableInvWavelength()返回一个int/100的结果也将是一个int

如果你必须有一个双结果,那么您必须存储双重结果。

double wavelength;

public void setWavelength(double value) {
    this.wavelength = value;
}  

pluggable.setWavelength(entry.getPluggableInvWavelength()/100.0);

除以 100.0 即可获得具有 2 位小数的双精度结果。

if entry.getPluggableInvWavelength() returnsd an int the results of /100 will also be an int

If you have to have a double result, then you must store a double result.

double wavelength;

public void setWavelength(double value) {
    this.wavelength = value;
}  

pluggable.setWavelength(entry.getPluggableInvWavelength()/100.0);

Dividing by 100.0 is all you need to have a double result with 2 decimal places.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文