Skip to content

Commit

Permalink
Fixed #317.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Aug 9, 2017
1 parent ef98bdb commit 7168d2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ public final class NumberUtil {
* @return 四舍五入后的整形值
*/
public static int roundHalfUp(final Object obj) {
if (obj instanceof Short) {
return (short) obj;
}
if (obj instanceof Integer) {
return (int) obj;
}
if (obj instanceof Long) {
return ((Long) obj).intValue();
}
if (obj instanceof Double) {
return new BigDecimal((double) obj).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,26 @@

public final class NumberUtilTest {

@Test
public void assertRoundHalfUpWithShort() {
short i = 1;
short j = 2;
assertThat(NumberUtil.roundHalfUp(i), is(1));
assertThat(NumberUtil.roundHalfUp(j), is(2));
}

@Test
public void assertRoundHalfUpWithInteger() {
assertThat(NumberUtil.roundHalfUp(1), is(1));
assertThat(NumberUtil.roundHalfUp(2), is(2));
}

@Test
public void assertRoundHalfUpWithLong() {
assertThat(NumberUtil.roundHalfUp(1L), is(1));
assertThat(NumberUtil.roundHalfUp(2L), is(2));
}

@Test
public void assertRoundHalfUpWithDouble() {
assertThat(NumberUtil.roundHalfUp(1.499d), is(1));
Expand Down

0 comments on commit 7168d2a

Please sign in to comment.