Integer or double return value

I passed the value of Integer, and then it is divided by 100, so the result can be either int or double, so I'm not sure if you use it or not.

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

then the value is divided by 100

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

So not sure how to use this value / object

+4
source share
4 answers

if entry.getPluggableInvWavelength () returns d int , the results of /100 will also be int

If you need a double result, you must save 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.

0
source

If you divide the integer ( int ) by another integer ( int ), the result will be an integer ( int ). - More: 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); 

Note that java.lang.Integer is an immutable wrapper type, not int ! - In fact, you cannot calculate using java.lang.Integer , but with Java 1.5, the compiler will convert int to Integer and vice versa automatically (automatic boxing and auto unpacking). But in general, it is better to understand the difference and use Integer only if you need real objects (and not numbers to calculate).

+4
source

If waveLength is double , then:

 entry.getPluggableWavelength() / 100d; 

d means that the number is treated as double , and therefore the result of dividing double .

+2
source

If you divide int by int , you will always get int . If you want a float or double (because you need to represent the fractional parts of the result), you will need to apply 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 
+1
source

All Articles