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
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).
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.
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
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.
Related
For this code block:
int num = 5;
int denom = 7;
double d = num / denom;
the value of d is 0.0. It can be forced to work by casting:
double d = ((double) num) / denom;
But is there another way to get the correct double result? I don't like casting primitives, who knows what may happen.
double num = 5;
That avoids a cast. But you'll find that the cast conversions are well-defined. You don't have to guess, just check the JLS. int to double is a widening conversion. From §5.1.2:
Widening primitive conversions do not
lose information about the overall
magnitude of a numeric value.
[...]
Conversion of an int or a long value
to float, or of a long value to
double, may result in loss of
precision-that is, the result may lose
some of the least significant bits of
the value. In this case, the resulting
floating-point value will be a
correctly rounded version of the
integer value, using IEEE 754
round-to-nearest mode (§4.2.4).
5 can be expressed exactly as a double.
What's wrong with casting primitives?
If you don't want to cast for some reason, you could do
double d = num * 1.0 / denom;
I don't like casting primitives, who knows what may happen.
Why do you have an irrational fear of casting primitives? Nothing bad will happen when you cast an int to a double. If you're just not sure of how it works, look it up in the Java Language Specification. Casting an int to double is a widening primitive conversion.
You can get rid of the extra pair of parentheses by casting the denominator instead of the numerator:
double d = num / (double) denom;
If you change the type of one the variables you have to remember to sneak in a double again if your formula changes, because if this variable stops being part of the calculation the result is messed up. I make a habit of casting within the calculation, and add a comment next to it.
double d = 5 / (double) 20; //cast to double, to do floating point calculations
Note that casting the result won't do it
double d = (double)(5 / 20); //produces 0.0
Type Casting Is The Only Way
May be you will not do it explicitly but it will happen.
Now, there are several ways we can try to get precise double value (where num and denom are int type, and of-course with casting)-
with explicit casting:
double d = (double) num / denom;
double d = ((double) num) / denom;
double d = num / (double) denom;
double d = (double) num / (double) denom;
but not double d = (double) (num / denom);
with implicit casting:
double d = num * 1.0 / denom;
double d = num / 1d / denom;
double d = ( num + 0.0 ) / denom;
double d = num; d /= denom;
but not double d = num / denom * 1.0;
and not double d = 0.0 + ( num / denom );
Now if you are asking- Which one is better? explicit? or implicit?
Well, lets not follow a straight answer here. Simply remember- We programmers don't like surprises or magics in a source. And we really hate Easter Eggs.
Also, an extra operation will definitely not make your code more efficient. Right?
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. double d = (double)5 / 20;
2. double v = (double)5 / (double) 20;
3. double v = 5 / (double) 20;
Note that casting the result won't do it. Because first division is done as per precedence rule.
double d = (double)(5 / 20); //produces 0.0
I do not think there is any problem with casting as such you are thinking about.
use something like:
double step = 1d / 5;
(1d is a cast to double)
Best way to do this is
int i = 3;
Double d = i * 1.0;
d is 3.0 now.
You might consider wrapping the operations. For example:
class Utils
{
public static double divide(int num, int denom) {
return ((double) num) / denom;
}
}
This allows you to look up (just once) whether the cast does exactly what you want. This method could also be subject to tests, to ensure that it continues to do what you want. It also doesn't matter what trick you use to cause the division (you could use any of the answers here), as long as it results in the correct result. Anywhere you need to divide two integers, you can now just call Utils::divide and trust that it does the right thing.
just use this.
int fxd=1;
double percent= (double)(fxd*40)/100;
Just add "D".
int i = 6;
double d = i / 2D; // This will divide bei double.
System.out.println(d); // This will print a double. = 3D
For this code block:
int num = 5;
int denom = 7;
double d = num / denom;
the value of d is 0.0. It can be forced to work by casting:
double d = ((double) num) / denom;
But is there another way to get the correct double result? I don't like casting primitives, who knows what may happen.
double num = 5;
That avoids a cast. But you'll find that the cast conversions are well-defined. You don't have to guess, just check the JLS. int to double is a widening conversion. From §5.1.2:
Widening primitive conversions do not
lose information about the overall
magnitude of a numeric value.
[...]
Conversion of an int or a long value
to float, or of a long value to
double, may result in loss of
precision-that is, the result may lose
some of the least significant bits of
the value. In this case, the resulting
floating-point value will be a
correctly rounded version of the
integer value, using IEEE 754
round-to-nearest mode (§4.2.4).
5 can be expressed exactly as a double.
What's wrong with casting primitives?
If you don't want to cast for some reason, you could do
double d = num * 1.0 / denom;
I don't like casting primitives, who knows what may happen.
Why do you have an irrational fear of casting primitives? Nothing bad will happen when you cast an int to a double. If you're just not sure of how it works, look it up in the Java Language Specification. Casting an int to double is a widening primitive conversion.
You can get rid of the extra pair of parentheses by casting the denominator instead of the numerator:
double d = num / (double) denom;
If you change the type of one the variables you have to remember to sneak in a double again if your formula changes, because if this variable stops being part of the calculation the result is messed up. I make a habit of casting within the calculation, and add a comment next to it.
double d = 5 / (double) 20; //cast to double, to do floating point calculations
Note that casting the result won't do it
double d = (double)(5 / 20); //produces 0.0
Type Casting Is The Only Way
May be you will not do it explicitly but it will happen.
Now, there are several ways we can try to get precise double value (where num and denom are int type, and of-course with casting)-
with explicit casting:
double d = (double) num / denom;
double d = ((double) num) / denom;
double d = num / (double) denom;
double d = (double) num / (double) denom;
but not double d = (double) (num / denom);
with implicit casting:
double d = num * 1.0 / denom;
double d = num / 1d / denom;
double d = ( num + 0.0 ) / denom;
double d = num; d /= denom;
but not double d = num / denom * 1.0;
and not double d = 0.0 + ( num / denom );
Now if you are asking- Which one is better? explicit? or implicit?
Well, lets not follow a straight answer here. Simply remember- We programmers don't like surprises or magics in a source. And we really hate Easter Eggs.
Also, an extra operation will definitely not make your code more efficient. Right?
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. double d = (double)5 / 20;
2. double v = (double)5 / (double) 20;
3. double v = 5 / (double) 20;
Note that casting the result won't do it. Because first division is done as per precedence rule.
double d = (double)(5 / 20); //produces 0.0
I do not think there is any problem with casting as such you are thinking about.
use something like:
double step = 1d / 5;
(1d is a cast to double)
Best way to do this is
int i = 3;
Double d = i * 1.0;
d is 3.0 now.
You might consider wrapping the operations. For example:
class Utils
{
public static double divide(int num, int denom) {
return ((double) num) / denom;
}
}
This allows you to look up (just once) whether the cast does exactly what you want. This method could also be subject to tests, to ensure that it continues to do what you want. It also doesn't matter what trick you use to cause the division (you could use any of the answers here), as long as it results in the correct result. Anywhere you need to divide two integers, you can now just call Utils::divide and trust that it does the right thing.
just use this.
int fxd=1;
double percent= (double)(fxd*40)/100;
Just add "D".
int i = 6;
double d = i / 2D; // This will divide bei double.
System.out.println(d); // This will print a double. = 3D
For this code block:
int num = 5;
int denom = 7;
double d = num / denom;
the value of d is 0.0. It can be forced to work by casting:
double d = ((double) num) / denom;
But is there another way to get the correct double result? I don't like casting primitives, who knows what may happen.
double num = 5;
That avoids a cast. But you'll find that the cast conversions are well-defined. You don't have to guess, just check the JLS. int to double is a widening conversion. From §5.1.2:
Widening primitive conversions do not
lose information about the overall
magnitude of a numeric value.
[...]
Conversion of an int or a long value
to float, or of a long value to
double, may result in loss of
precision-that is, the result may lose
some of the least significant bits of
the value. In this case, the resulting
floating-point value will be a
correctly rounded version of the
integer value, using IEEE 754
round-to-nearest mode (§4.2.4).
5 can be expressed exactly as a double.
What's wrong with casting primitives?
If you don't want to cast for some reason, you could do
double d = num * 1.0 / denom;
I don't like casting primitives, who knows what may happen.
Why do you have an irrational fear of casting primitives? Nothing bad will happen when you cast an int to a double. If you're just not sure of how it works, look it up in the Java Language Specification. Casting an int to double is a widening primitive conversion.
You can get rid of the extra pair of parentheses by casting the denominator instead of the numerator:
double d = num / (double) denom;
If you change the type of one the variables you have to remember to sneak in a double again if your formula changes, because if this variable stops being part of the calculation the result is messed up. I make a habit of casting within the calculation, and add a comment next to it.
double d = 5 / (double) 20; //cast to double, to do floating point calculations
Note that casting the result won't do it
double d = (double)(5 / 20); //produces 0.0
Type Casting Is The Only Way
May be you will not do it explicitly but it will happen.
Now, there are several ways we can try to get precise double value (where num and denom are int type, and of-course with casting)-
with explicit casting:
double d = (double) num / denom;
double d = ((double) num) / denom;
double d = num / (double) denom;
double d = (double) num / (double) denom;
but not double d = (double) (num / denom);
with implicit casting:
double d = num * 1.0 / denom;
double d = num / 1d / denom;
double d = ( num + 0.0 ) / denom;
double d = num; d /= denom;
but not double d = num / denom * 1.0;
and not double d = 0.0 + ( num / denom );
Now if you are asking- Which one is better? explicit? or implicit?
Well, lets not follow a straight answer here. Simply remember- We programmers don't like surprises or magics in a source. And we really hate Easter Eggs.
Also, an extra operation will definitely not make your code more efficient. Right?
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. double d = (double)5 / 20;
2. double v = (double)5 / (double) 20;
3. double v = 5 / (double) 20;
Note that casting the result won't do it. Because first division is done as per precedence rule.
double d = (double)(5 / 20); //produces 0.0
I do not think there is any problem with casting as such you are thinking about.
use something like:
double step = 1d / 5;
(1d is a cast to double)
Best way to do this is
int i = 3;
Double d = i * 1.0;
d is 3.0 now.
You might consider wrapping the operations. For example:
class Utils
{
public static double divide(int num, int denom) {
return ((double) num) / denom;
}
}
This allows you to look up (just once) whether the cast does exactly what you want. This method could also be subject to tests, to ensure that it continues to do what you want. It also doesn't matter what trick you use to cause the division (you could use any of the answers here), as long as it results in the correct result. Anywhere you need to divide two integers, you can now just call Utils::divide and trust that it does the right thing.
just use this.
int fxd=1;
double percent= (double)(fxd*40)/100;
Just add "D".
int i = 6;
double d = i / 2D; // This will divide bei double.
System.out.println(d); // This will print a double. = 3D
I want to divide a number by 100 and number is of Object type.
Object a = 33;
Object b = .33;
Double d = (Double) a / 100;
Double e = (Double) b / 100;
First, I had type cast to Double, but it's failing "Cannot cast numeric value to java.lang.Double" for Integer.
Do I need to check instance before dividing? I am new and not sure what is correct way to do it?
EDIT: I cannot avoid Object
If you must use objects (and I suggest you don't for a number of reasons) you could do this
Number a = 33;
Number b = 0.33;
Number d = a.doubleValue() / 100.0;
Number e = b.doubleValue() / 100.0;
However, you should really only use objects for numbers if
the value could be null
you are using it as a generic type e.g. List<Double>
you are using a type which doesn't have a primitive e.g. BigDecimal
You need to use doubles not Objects
final int a = 33;
final double b = 0.33;
final double d = a / 100.0;
final Double e = b / 100.0;
In Java you can cast primitives like int to a double but you can't do the same with the wrapper classes like Integer and Double.
In your example, 33 is autoboxed to an Integer, which cannot then be cast to a Double. In the specific case you can avoid the exception by the following way:
Object a = 33;
if(a instanceof Number) {
Double d = ((Number) a).doubleValue() / 100;
System.out.println(d);
}
But there is not the best way to solve you problem. Better to use primitive int and double, or theirs wrappers Integer and Double, but not Object.
For this code block:
int num = 5;
int denom = 7;
double d = num / denom;
the value of d is 0.0. It can be forced to work by casting:
double d = ((double) num) / denom;
But is there another way to get the correct double result? I don't like casting primitives, who knows what may happen.
double num = 5;
That avoids a cast. But you'll find that the cast conversions are well-defined. You don't have to guess, just check the JLS. int to double is a widening conversion. From §5.1.2:
Widening primitive conversions do not
lose information about the overall
magnitude of a numeric value.
[...]
Conversion of an int or a long value
to float, or of a long value to
double, may result in loss of
precision-that is, the result may lose
some of the least significant bits of
the value. In this case, the resulting
floating-point value will be a
correctly rounded version of the
integer value, using IEEE 754
round-to-nearest mode (§4.2.4).
5 can be expressed exactly as a double.
What's wrong with casting primitives?
If you don't want to cast for some reason, you could do
double d = num * 1.0 / denom;
I don't like casting primitives, who knows what may happen.
Why do you have an irrational fear of casting primitives? Nothing bad will happen when you cast an int to a double. If you're just not sure of how it works, look it up in the Java Language Specification. Casting an int to double is a widening primitive conversion.
You can get rid of the extra pair of parentheses by casting the denominator instead of the numerator:
double d = num / (double) denom;
If you change the type of one the variables you have to remember to sneak in a double again if your formula changes, because if this variable stops being part of the calculation the result is messed up. I make a habit of casting within the calculation, and add a comment next to it.
double d = 5 / (double) 20; //cast to double, to do floating point calculations
Note that casting the result won't do it
double d = (double)(5 / 20); //produces 0.0
Type Casting Is The Only Way
May be you will not do it explicitly but it will happen.
Now, there are several ways we can try to get precise double value (where num and denom are int type, and of-course with casting)-
with explicit casting:
double d = (double) num / denom;
double d = ((double) num) / denom;
double d = num / (double) denom;
double d = (double) num / (double) denom;
but not double d = (double) (num / denom);
with implicit casting:
double d = num * 1.0 / denom;
double d = num / 1d / denom;
double d = ( num + 0.0 ) / denom;
double d = num; d /= denom;
but not double d = num / denom * 1.0;
and not double d = 0.0 + ( num / denom );
Now if you are asking- Which one is better? explicit? or implicit?
Well, lets not follow a straight answer here. Simply remember- We programmers don't like surprises or magics in a source. And we really hate Easter Eggs.
Also, an extra operation will definitely not make your code more efficient. Right?
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. double d = (double)5 / 20;
2. double v = (double)5 / (double) 20;
3. double v = 5 / (double) 20;
Note that casting the result won't do it. Because first division is done as per precedence rule.
double d = (double)(5 / 20); //produces 0.0
I do not think there is any problem with casting as such you are thinking about.
use something like:
double step = 1d / 5;
(1d is a cast to double)
Best way to do this is
int i = 3;
Double d = i * 1.0;
d is 3.0 now.
You might consider wrapping the operations. For example:
class Utils
{
public static double divide(int num, int denom) {
return ((double) num) / denom;
}
}
This allows you to look up (just once) whether the cast does exactly what you want. This method could also be subject to tests, to ensure that it continues to do what you want. It also doesn't matter what trick you use to cause the division (you could use any of the answers here), as long as it results in the correct result. Anywhere you need to divide two integers, you can now just call Utils::divide and trust that it does the right thing.
just use this.
int fxd=1;
double percent= (double)(fxd*40)/100;
Just add "D".
int i = 6;
double d = i / 2D; // This will divide bei double.
System.out.println(d); // This will print a double. = 3D