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
Double value negative after multiplication -Java - Stack Overflow
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