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) {
|
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;
|
return Result.POSITIVE;
|
||||||
} else if (result == -1.0f) {
|
} else if (result.compareTo(-1.0f) == 0) {
|
||||||
return Result.NEGATIVE;
|
return Result.NEGATIVE;
|
||||||
}
|
}
|
||||||
return Result.ZERO;
|
return Result.ZERO;
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package com.baeldung.positivenegative;
|
package com.baeldung.positivenegative;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import static com.baeldung.positivenegative.PositiveOrNegative.Result.NEGATIVE;
|
||||||
|
import static com.baeldung.positivenegative.PositiveOrNegative.Result.POSITIVE;
|
||||||
import static com.baeldung.positivenegative.PositiveOrNegative.Result.*;
|
import static com.baeldung.positivenegative.PositiveOrNegative.Result.ZERO;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class PositiveOrNegativeUnitTest {
|
class PositiveOrNegativeUnitTest {
|
||||||
@Test
|
@Test
|
||||||
void givenIntegers_whenChkPositiveOrNegativeByOperator_thenReturnExpectedResult() {
|
void givenIntegers_whenChkPositiveOrNegativeByOperator_thenReturnExpectedResult() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user