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:
Kai Yuan 2022-09-10 17:06:11 +02:00 committed by GitHub
parent ecfe4a05a2
commit 1b334f6a2d
2 changed files with 8 additions and 6 deletions

View File

@ -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;

View File

@ -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() {