[Java] Something is displayed as "-0.0" in the output

The trigger was by chance

When I did java with IntelliJ as I wanted, I got "Hmm?"

output.console


value = -0.0

Conclusion

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

Coping

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

Postscript 2020/05/26

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

[Java] Something is displayed as "-0.0" in the output
Output Notes document as XML document in Java
What is the main method in Java?
This is the Excel output in Java! "JXLS" official document translation
The intersection type introduced in Java 10 is amazing (?)
When the project is not displayed in eclipse
Which is better, Kotlin or Java in the future?
[Error] The app is not displayed in the production environment
Java11: Run Java code in a single file as is
The story that .java is also built in Unity 2018
Java is the 5th day
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
Implement the same function as C, C ++ system ("cls"); in Java
Access the network interface in Java
Guess the character code in Java
Specify the java location in eclipse.ini
Where is the Java LocalDateTime.now () timezone?
Unzip the zip file in Java
Mixed Western calendar output in Java
Log output to file in Java
Parsing the COTOHA API in Java
Call the super method in Java
Output the difference between each field of two objects in Java
How to output the value when there is an array in the array
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
What is a class in Java language (3 /?)
Get the result of POST in Java
Spring Autowired is written in the constructor
Try using the Stream API in Java
Call the Windows Notification API in Java
I tried the new era in Java
Memo: [Java] If a file is in the monitored directory, process it.
[Java] Use cryptography in the standard library
Organized memo in the head (Java --Array)
What is the best file reading (Java)
What is a class in Java language (1 /?)
Try calling the CORBA service in Java 11+
[Rails] About the error that the image is not displayed in the production environment
What is a class in Java language (2 /?)
Output Date in Java in ISO 8601 extended format
The Java EE Security API is here!
How to get the date in java
Output of the book "Introduction to Java"
Implement something like a stack in Java
Image is not displayed in production environment
The story of writing Java in Emacs
Console input in Java (understanding the mechanism)
[Java] Color the standard output to the terminal
[Cloud9] Yay! You ’re on Rails! Is not displayed in the rails tutorial
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
File output bean as JSON in spring
[Deep Learning from scratch] 2. There is no such thing as NumPy in Java.
Possibility when deploying to EC2 but nothing is displayed in the error log
[Java] Where is the implementation class of annotation that exists in Bean Validation?
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
I can't remember the text file input / output in Java, so I summarized it.
What is ... (3 dots) found in the Java source? Variadic arguments to know from
Is Ruby's addition, subtraction, multiplication and division the same as in other languages?
What is Pullback doing in The Composable Architecture
Regarding the transient modifier and serialization in Java