Positive or negative (#12706)
* Check if a number is positive or negative in Java * reformat single line if/else statements * Check if a number is positive or negative in Java * [positiveOrNegative] using compareTo() instead of == to compare float numbers * [positiveOrNegative] re-org imports
This commit is contained in:
parent
ecfe4a05a2
commit
1b334f6a2d
|
@ -26,11 +26,11 @@ public class PositiveOrNegative {
|
|||
}
|
||||
|
||||
public static Result bySignum(Float floatNumber) {
|
||||
float result = Math.signum(floatNumber);
|
||||
Float result = Math.signum(floatNumber);
|
||||
|
||||
if (result == 1.0f) {
|
||||
if (result.compareTo(1.0f) == 0) {
|
||||
return Result.POSITIVE;
|
||||
} else if (result == -1.0f) {
|
||||
} else if (result.compareTo(-1.0f) == 0) {
|
||||
return Result.NEGATIVE;
|
||||
}
|
||||
return Result.ZERO;
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package com.baeldung.positivenegative;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.baeldung.positivenegative.PositiveOrNegative.Result.*;
|
||||
import static com.baeldung.positivenegative.PositiveOrNegative.Result.NEGATIVE;
|
||||
import static com.baeldung.positivenegative.PositiveOrNegative.Result.POSITIVE;
|
||||
import static com.baeldung.positivenegative.PositiveOrNegative.Result.ZERO;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PositiveOrNegativeUnitTest {
|
||||
@Test
|
||||
void givenIntegers_whenChkPositiveOrNegativeByOperator_thenReturnExpectedResult() {
|
||||
|
|
Loading…
Reference in New Issue