This commit is related to BAEL-7113 (#15331)
This commit adds a new test class "RoundNumberToNearestMultipleUnitTest" that suggests multiplе mеthods for rounding up a numbеr to thе nеarеst multiplе of 5 in Java.
This commit is contained in:
parent
3643c289c7
commit
d7bb6bc0a7
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.roundnumbertonearestmultiple;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class RoundNumberToNearestMultipleUnitTest {
|
||||
|
||||
public static int originalNumber = 18;
|
||||
public static int expectedRoundedNumber = 20;
|
||||
public static int nearest = 5;
|
||||
|
||||
@Test
|
||||
public void givenNumber_whenUsingBasicMathOperations_thenRoundUpToNearestMultipleOf5() {
|
||||
|
||||
int roundedNumber = (originalNumber % nearest == 0) ? originalNumber : ((originalNumber / nearest) + 1) * nearest;
|
||||
assertEquals(expectedRoundedNumber, roundedNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumber_whenUsingMathCeil_thenRoundUpToNearestMultipleOf5() {
|
||||
|
||||
int roundedNumber = (int) (Math.ceil(originalNumber / (float) (nearest)) * nearest);
|
||||
assertEquals(expectedRoundedNumber, roundedNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumber_whenUsingMathRound_thenRoundUpToNearestMultipleOf5() {
|
||||
|
||||
int roundedNumber = Math.round(originalNumber / (float) (nearest)) * nearest;
|
||||
assertEquals(expectedRoundedNumber, roundedNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumber_whenUsingMathFloor_thenRoundUpToNearestMultipleOf5() {
|
||||
|
||||
int roundedNumber = (int) (Math.floor((double) (originalNumber + nearest / 2) / nearest) * nearest);
|
||||
assertEquals(expectedRoundedNumber, roundedNumber);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue