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:
MohamedHelmyKassab 2023-12-01 02:59:17 +02:00 committed by GitHub
parent 3643c289c7
commit d7bb6bc0a7
1 changed files with 41 additions and 0 deletions

View File

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