When Math.signum(x) == 0.

All other attempts to check whether float x == 0 may fail.

But Math.signum() is so basic, it should never fail.

Answer from JonAar Livernois on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Float.html
Float (Java Platform SE 8 )
July 21, 2026 - the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is numerically greater than f2. ... Adds two float values together as per the + operator. ... Returns the greater of two float values as if by calling Math.max.
Top answer
1 of 5
79

Because Java uses the IEEE Standard for Floating-Point Arithmetic (IEEE 754) which defines -0.0 and when it should be used.

The smallest number representable has no 1 bit in the subnormal significand and is called the positive or negative zero as determined by the sign. It actually represents a rounding to zero of numbers in the range between zero and the smallest representable non-zero number of the same sign, which is why it has a sign, and why its reciprocal +Inf or -Inf also has a sign.

You can get around your specific problem by adding 0.0

e.g.

Double.toString(value + 0.0);

See: Java Floating-Point Number Intricacies

Operations Involving Negative Zero
...
(-0.0) + 0.0 -> 0.0

-

"-0.0" is produced when a floating-point operation results in a negative floating-point number so close to 0 that it cannot be represented normally.

2 of 5
21

how come a primitive float value can be -0.0?

floating point numbers are stored in memory using the IEEE 754 standard meaning that there could be rounding errors. You could never be able to store a floating point number of infinite precision with finite resources.

You should never test if a floating point number == to some other, i.e. never write code like this:

if (a == b)

where a and b are floats. Due to rounding errors those two numbers might be stored as different values in memory.

You should define a precision you want to work with:

private final static double EPSILON = 0.00001;

and then test against the precision you need

if (Math.abs(a - b) < epsilon)

So in your case if you want to test that a floating point number equals to zero in the given precision:

if (Math.abs(a) < epsilon)

And if you want to format the numbers when outputting them in the GUI you may take a look at the following article and the NumberFormat class.

🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Float.html
Float (Java Platform SE 7 )
The sign of the integer value returned is the same as that of the integer that would be returned by the call: ... the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is numerically greater than f2.
🌐
Oracle
docs.oracle.com › javase › 1.5.0 › docs › api › java › lang › Float.html
Float (Java 2 Platform SE 5.0)
The sign of the integer value returned is the same as that of the integer that would be returned by the call: ... the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is numerically greater than f2.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Float.html
Float (Java SE 17 & JDK 17)
April 21, 2026 - If m is zero, it is represented by the string "0x0.0p0"; thus, negative zero produces the result "-0x0.0p0" and positive zero produces the result "0x0.0p0". If m is a float value with a normalized representation, substrings are used to represent the significand and exponent fields.
🌐
Study.com
study.com › business courses › java programming tutorial & training
Java: Floating Point Numbers - Lesson | Study.com
October 3, 2018 - Both float and double data types can store positive and negative values. To unlock this lesson you must be a Study.com member Create an account · A float data type in Java stores a decimal value with 6-7 total digits of precision.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Float.html
Float (Java Platform SE 6)
The sign of the integer value returned is the same as that of the integer that would be returned by the call: ... the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is numerically greater than f2.
🌐
Java67
java67.com › 2015 › 09 › float-and-double-value-comparison-in-java-use-relational.html
Why you shouldn't use == with float and double in Java? | Java67
As we have seen in the first paragraph that use of == operator can cause an endless loop in Java, but is there a way to prevent that loop from running infinitely? Yes, instead of using equality operator (==), you can use relational operator e.g. less than (<) or greater than (>) to compare float and double values. By changing the above code as following, you can prevent the loop from infinitely : for(double balance = 10; balance > 0; balance-=0.1) { System.out.println(balance); } If you think a little bit then you will realize that greater than will definitely end this loop, but don't expect it to print numbers like 9.9, 9.8, 9.7 because floating-point numbers are a just approximation, they are not exact.
🌐
Quora
quora.com › Java-Programming-Language-Why-does-float-var-1-0-generate-a-type-mismatch
Java Programming Language: Why does 'float var = 1.0' generate a type mismatch? - Quora
Answer (1 of 6): Because 1.0 is a double literal. This means that you ask the compiler to assign a double to a float and this is forbidden. In order to do what you mean, you need to use a float literal: 1.0f. The trailing f makes the difference: it tells the compiler that the number has float ...
🌐
iO Flood
ioflood.com › blog › java-float
Java Float Keyword Explained: Usage and Examples
February 26, 2024 - In the context of Java’s float, it’s a single-precision 32-bit IEEE 754 floating point. This means it uses 1 bit for sign, 8 bits for exponent, and 23 bits for the fraction. Let’s see a simple example of this representation: float num = 1.5f; // In memory, 1.5 is represented as follows: // Sign: 0 (as it's a positive number) // Exponent: 127 (bias for single-precision) // Fraction: 10011001100110011001101 (binary representation of 1.5)
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › lang › Float.java
jdk/src/java.base/share/classes/java/lang/Float.java at master · openjdk/jdk
* When preview features are enabled, {@code Float} is a {@linkplain Class#isValue value class}. * Use of value class instances for synchronization or with · * {@linkplain java.lang.ref.Reference object references} result in
Author: openjdk
🌐
Oracle
docs.oracle.com › en › java › javase › 23 › docs › api › java.base › java › lang › Float.html
Float (Java SE 23 & JDK 23)
October 17, 2024 - If m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0". Otherwise m is positive and finite. It is converted to a string in two stages: Selection of a decimal: A well-defined decimal dm is selected to ...
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › › java.base › java › lang › Float.html
Float (Java SE 21 & JDK 21)
January 20, 2026 - If m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0". Otherwise m is positive and finite. It is converted to a string in two stages: Selection of a decimal: A well-defined decimal dm is selected to ...
🌐
Oracle
docs.oracle.com › en › java › javase › 24 › docs › api › java.base › java › lang › Float.html
Float (Java SE 24 & JDK 24)
July 15, 2025 - If m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0". Otherwise m is positive and finite. It is converted to a string in two stages: Selection of a decimal: A well-defined decimal dm is selected to ...
🌐
W3Schools
w3schools.com › java › ref_keyword_float.asp
Java float Keyword
The float keyword is a data type that can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f": Read more about data types in our Java Data Types Tutorial.
🌐
GeeksforGeeks
geeksforgeeks.org › java › float-equals-method-in-java-with-examples
Float equals() method in Java with examples - GeeksforGeeks
July 11, 2025 - If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true. Below programs illustrates the use of Float.equals() method: Program 1: java ·