Being pedantic, < 0 won't give you all negative numbers.

double d = -0.0;
System.out.println(d + " compared with 0.0 is " + Double.compare(d, 0.0));
System.out.println(d + " < 0.0 is " + (d < 0.0));

prints

-0.0 compared with 0.0 is -1
-0.0 < 0.0 is false

-0.0 is negative but not less than 0.0

You can use

public static boolean isNegative(double d) {
     return Double.compare(d, 0.0) < 0;
}

A more efficient, if more obtuse, version is to check the signed bit.

public static boolean isNegative(double d) {
     return Double.doubleToRawLongBits(d) < 0;
}

Note: Under IEEE-754 a NaN can have the same signed bit as a negative number.

Answer from Peter Lawrey on Stack Overflow
🌐
Quora
quora.com › How-do-you-check-if-a-double-value-is-negative-or-not-in-Java
How to check if a double value is negative or not in Java - Quora
Answer (1 of 4): Negative just means less than zero in pretty much any number system, including the system that doubles represent. So the following Java method would work: [code]public boolean isNegative(double value) { if(double
Discussions

Java Double variable refuses to go negative after subtraction - Stack Overflow
Floating point numbers are best ...operworks/java/library/j-jtp0114 2011-03-24T23:38:28.267Z+00:00 ... @Jason - get rid of all double variables and replace them with BigDecimal 2011-03-24T23:55:44.217Z+00:00 ... I suggest you step through your code using a debugger and you would see how your values are actually changing. Your negative interest is ... More on stackoverflow.com
🌐 stackoverflow.com
November 7, 2011
java - Correct Way to Obtain The Most Negative Double - Stack Overflow
Is this the correct way to obtain the most negative double in Java? double v = -Double.MAX_VALUE; More on stackoverflow.com
🌐 stackoverflow.com
double negation
If we're talking JavaScript, then it's to "cast" things to boolean values. More on reddit.com
🌐 r/learnjava
5
3
May 20, 2023
Java doubles and 0.0 - GameDev.net Forums
Does it do two checks if you do a zero comparison with a double? ... For pretty much everything that you try and fail, you can safely assume someone else tried and failed before, and wrote about it at the Internet. First hit was https://stackoverflow.com/questions/39933792/how-do-i-check-for-a-negative-zero-in-java... More on gamedev.net
🌐 gamedev.net
January 14, 2024
🌐
Baeldung
baeldung.com › home › java › java numbers › check if a number is positive or negative in java
Check if a Number Is Positive or Negative in Java | Baeldung
January 8, 2024 - However, the Math.signum() method accepts Float and Double numbers as the parameter, for example: static Result bySignum(Float floatNumber) { float result = Math.signum(floatNumber); if (result.compareTo(1.0f) == 0) { return Result.POSITIVE; } else if (result.compareTo(-1.0f) == 0) { return Result.NEGATIVE; } return Result.ZERO; }
🌐
Blogger
javarevisited.blogspot.com › 2013 › 01 › how-to-check-if-number-is-positive-or-negative-java-example.html
How to check if a Number is Positive or Negative in Java? Solution Example
Use relational operator to check if a number is positive or not, if number >=0 means a number is positive if number<0 means number is negative in Java this should work for double and float as well but I haven't tested it for all values.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Double.html
Double (Java Platform SE 7 )
If the argument is NaN, the result is the string "NaN". Otherwise, the result is a string that represents the sign and magnitude of the argument. If the sign is negative, the first character of the result is '-' ('\u002D'); if the sign is positive, no sign character appears in the result.
🌐
Programming.Guide
programming.guide › java › integer-minvalue-negative-double-minvalue-positive.html
Java: Why's Double.MIN_VALUE is positive? Integer.MIN_VALUE is negative! | Programming.Guide
See all 190 Java articles · Dynamic ... Double.MIN_VALUE holds the value 2-1074. This is a positive value, as opposed to Integer.MIN_VALUE which holds the negative value -2147483648....
Find elsewhere
🌐
Apache
db.apache.org › derby › docs › 10.1 › ref › rrefsqljdoubleprecision.html
DOUBLE PRECISION
Largest negative DOUBLE value: -2.225E-307 · These limits are different from the java.lang.DoubleJava type limits. An exception is thrown when any double value is calculated or entered that is outside of these value ranges. Arithmetic operations do not round their resulting values to zero.
🌐
Java-forums
java-forums.org › new-java › 43411-double-no-negative-values.html
Double with no negative values.
May 3, 2011 - The Java Language Environment Short answer: no. You can use conditionals to prevent it from being negative though. Fun: Trolling is a art | NYANYANYANYANYAN Helpful: Book Recommendations | Recursion ... From my understanding you wouldn't declare 'double not negative' but declare a double and ...
🌐
Programiz
programiz.com › java-programming › examples › positive-negative
Java Program to Check Whether a Number is Positive or Negative
Java Operators · public class PositiveNegative { public static void main(String[] args) { double number = 12.3; // true if number is less than 0 if (number < 0.0) System.out.println(number + " is a negative number."); // true if number is greater than 0 else if ( number > 0.0) System.out.println(number + " is a positive number."); // if both test expression is evaluated to false else System.out.println(number + " is 0."); } } Output ·
🌐
Javatpoint
javatpoint.com › java-program-to-check-if-a-number-is-positive-or-negative
Java Program to Check if a Number is Positive or Negative - Javatpoint
Java BigDecimal class · abs() add() divide() doubleValue() equals() floatValue() hashCode() intValue() intValueExact() max() min() movePointLeft() movePointRight() multiply() negate() Big Integer Class ·
🌐
GameDev.net
gamedev.net › home › forums › for beginners › java doubles and 0.0
Java doubles and 0.0 - GameDev.net Forums
January 14, 2024 - You can read the basis of it for Java 21 here, but this specific behavior has been the same since the beginning of the language. Look down a few paragraphs to read “Positive zero and negative zero compare equal, so the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false.
🌐
DocStore
docstore.mik.ua › orelly › java › langref › ch10_08.htm
[Chapter 10] Double
home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam
🌐
TutorialsPoint
tutorialspoint.com › get-the-absolute-value-of-float-int-double-and-long-in-java
Java - Math abs(float) Method
June 26, 2020 - The Java Math abs(float a) returns the absolute value of a float value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.Special cases − Following is the declaration for
🌐
IPFW
cs.ait.ac.th › ~on › O › oreilly › java › langref › ch10_08.htm
[Chapter 10] Double
This method returns a String object that contains a representation of the given double value. The values NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY, -0.0, and +0.0 are represented by the strings "NaN", "--Infinity", "Infinity", "--0.0", and "0.0", respectively.
Top answer
1 of 3
4

After bidAmount = Double.parseDouble(messageToServer);, we need to add the following:

if(bidAmount < 0){
  throw new NumberFormatException("Invalid amount");
}

An alternate solution (and probably, best practice) would be to modify catch block to catch IllegalArgumentException and throw IllegalArgumentException if the amount is less than 0 as shown below:

try
        {
            //convert number to double (monetary value)
            bidAmount = Double.parseDouble(messageToServer);
            if(bidAmount < 0){
                throw new IllegalArgumentException("Invalid amount");
            }
            //send to server with item code
            output.println(selectedItemCode + bidAmount);
        }
        //item cannot turned to double
        catch (IllegalArgumentException numfEx)
        {
            //inform user
            fromServer.setText("Invalid Request!"
                    + USER_PROMPT);
        }
2 of 3
1

This can be achieved without relying on exceptions to control the flow of your program, which is generally a bad idea - see discussion here

else {   
    // using a tempBidAmount as I don't have enough context to know whether
    // I could just assign the output of parseUserDouble directly to bidAmount
    // without causing issues
    Double tempBidAmount = parseUserDouble(messageToServer);

    if (tempBidAmount != null && tempBidAmount > 0.0) {
        bidAmount = tempBidAmount;
        output.println(selectedItemCode + bidAmount);
    } else {
        fromServer.setText("Invalid Request!" + USER_PROMPT);
    }
}
toServer.setText("");

....

private Double parseUserDouble(Sting userInput) {
    try {
        return Double.parseDouble(userInput);
    }
    catch (NumberFormatException numfEx) {
        // consider logging here        
    }
    return null;
}