Skip to content

Commit 6391285

Browse files
committed
Added Math.ceil method to round up.
1 parent 21ed59c commit 6391285

2 files changed

Lines changed: 20 additions & 26 deletions

File tree

‎algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundred.java‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44

55
public class RoundUpToHundred {
66

7-
public static void main(String[] args) {
8-
9-
Scanner scanner = new Scanner(System.in);
10-
double input = scanner.nextDouble();
11-
scanner.close();
12-
13-
RoundUpToHundred.round(input);
14-
}
15-
16-
static int round(double input) {
17-
18-
int i = (int) Math.round(input);
19-
return ((i + 99) / 100) * 100;
20-
};
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in);
9+
double input = scanner.nextDouble();
10+
scanner.close();
11+
12+
RoundUpToHundred.round(input);
13+
}
14+
15+
static int round(double input) {
16+
int i = (int) Math.ceil(input);
17+
return ((i + 99) / 100) * 100;
18+
};
2119

2220
}

‎algorithms/roundUpToHundred/src/com/java/src/RoundUpToHundredTest.java‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
import static org.junit.Assert.assertEquals;
44

5-
public class RoundUpToHundredTest {
6-
public void roundupTest() {
7-
8-
assertEquals("Rounded up to hundred", 100,
9-
RoundUpToHundred.round(99));
10-
11-
assertEquals("Rounded down to two hundred ", 200,
12-
RoundUpToHundred.round(200.2));
5+
import org.junit.Test;
136

14-
assertEquals("Returns same rounded value", 300,
15-
RoundUpToHundred.round(300));
16-
17-
}
7+
public class RoundUpToHundredTest {
8+
@Test
9+
public void givenInput_whenRoundedUp_thenTrue() {
10+
assertEquals("Rounded up to hundred", 100, RoundUpToHundred.round(99));
11+
assertEquals("Rounded up to three hundred ", 300, RoundUpToHundred.round(200.2));
12+
assertEquals("Returns same rounded value", 400, RoundUpToHundred.round(400));
13+
}
1814
}

0 commit comments

Comments
 (0)