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 OverflowBeing 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.
Double v = (Double.parseDouble(data[2]));
if (v<0){
//do whatever?
}
Java Double variable refuses to go negative after subtraction - Stack Overflow
java - Correct Way to Obtain The Most Negative Double - Stack Overflow
double negation
Java doubles and 0.0 - GameDev.net Forums
I don't know what the exact issue is here, but use BigDecimal (instead of double) when working with money.
I suggest you step through your code using a debugger and you would see how your values are actually changing.
Your negative interest is interest_neg = -0.002. which you multiply by your amount.
Say you have the following situation
double balance = -100;
if ( balance >= 0 ) balance *= interest_pos;
else balance *= interest_neg; // balance = -100 * -0.002 = +0.2
A negative interest doesn't make any sense of this value as you can see. If I had a million dollar debt, the next month I would have $200. The more in debt I am the more money I will have next month.
So whenever you have a negative balance you are making it positive again. Perhaps your negative interest should be 1.002, similar to your positive interest.
else balance *= interest_neg; // balance = -100 * 1.002 = -100.2
I have seen some developers double negate...example
!!true
....why do people do this???
(int)1700 * (int)1944038 is equal to your -990102696.
Are you sure c12 and fileSize aren't integers? If they are, the multiplication occurs with integer types, integer overflow and it is being promoted to double afterwards.
Your c12 and fileSize are most likely ints (+1 Tomasz). Java multiplies the ints, which overflows, becoming negative, and then stores that negative value in your double. Cast c12 and fileSize to double before multiplying:
double c12 = 1700, fileSize = 1944038;
System.out.println(c12 * fileSize);
produces:
3.3048646E9
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);
}
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;
}