Reputation: 2353
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
Upvotes: 4
Views: 4621
Reputation: 533530
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.
Upvotes: 0
Reputation: 120811
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).
Upvotes: 4
Reputation: 272517
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
Upvotes: 1
Reputation: 597114
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
.
Upvotes: 2