When I did java with IntelliJ as I wanted, I got "Hmm?"
output.console
value = -0.0
Dividing 0 by a negative number seems to be -0.0
sample.java
public class Main {
public static void main(String[] args) {
double value = 0;
System.out.println("value = "+value/-1);
System.out.printf("value = %3.1f\n",value/-5);
}
}
output.console
value = -0.0
value = -0.0
Do you want to add an if statement? If you substitute 0 in the if statement for the value to be divided, the display will be correct.
sample.java
public class Main {
public static void main(String[] args) {
double value = 0;
value /= -1;
System.out.println("Before dealing= "+value);
if(value == 0) value =0;
System.out.println("After dealing= "+value);
}
}
output.console
Before dealing= -0.0
After dealing= 0.0
You can correct it by adding "+0" to the calculation that may give -0.
sample.java
public class Main {
public static void main(String[] args) {
double value = 0;
value /= -1 +0;//+0 then-Does not become 0
}
}
Recommended Posts