提交 aac5e958 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #498 from yuanwhy/fix-round-function

Fix h2 ROUND
......@@ -1234,7 +1234,11 @@ public class Function extends Expression implements FunctionCall {
break;
case ROUND: {
double f = v1 == null ? 1. : Math.pow(10., v1.getDouble());
result = ValueDouble.get(Math.round(v0.getDouble() * f) / f);
double middleResult = v0.getDouble() * f;
int oneWithSymbol = middleResult > 0 ? 1 : -1;
result = ValueDouble.get(Math.round(Math.abs(middleResult)) / f * oneWithSymbol);
break;
}
case TRUNCATE: {
......
......@@ -117,6 +117,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
testThatCurrentTimestampStaysTheSameWithinATransaction();
testThatCurrentTimestampUpdatesOutsideATransaction();
testAnnotationProcessorsOutput();
testRound();
deleteDb("functions");
}
......@@ -2010,6 +2011,28 @@ public class TestFunctions extends TestBase implements AggregateFunction {
}
}
private void testRound() throws SQLException {
deleteDb("functions");
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
final ResultSet rs = stat.executeQuery(
"select ROUND(-1.2), ROUND(-1.5), ROUND(-1.6), ROUND(2), ROUND(1.5), ROUND(1.8), ROUND(1.1) from dual");
rs.next();
assertEquals(-1, rs.getInt(1));
assertEquals(-2, rs.getInt(2));
assertEquals(-2, rs.getInt(3));
assertEquals(2, rs.getInt(4));
assertEquals(2, rs.getInt(5));
assertEquals(2, rs.getInt(6));
assertEquals(1, rs.getInt(7));
rs.close();
conn.close();
}
private void testThatCurrentTimestampIsSane() throws SQLException,
ParseException {
deleteDb("functions");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论