diff --git a/deps/common/lang/cmath.h b/deps/common/lang/cmath.h new file mode 100644 index 000000000..f25cfcef2 --- /dev/null +++ b/deps/common/lang/cmath.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved. +miniob is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. */ +#pragma once + +#include + +using std::round; \ No newline at end of file diff --git a/deps/common/lang/string.cpp b/deps/common/lang/string.cpp index de8c7af1e..11a8d3593 100644 --- a/deps/common/lang/string.cpp +++ b/deps/common/lang/string.cpp @@ -22,6 +22,7 @@ See the Mulan PSL v2 for more details. */ #include "common/log/log.h" #include "common/lang/algorithm.h" +#include "common/lang/cmath.h" #include "common/lang/iomanip.h" namespace common { @@ -270,7 +271,8 @@ char *substr(const char *s, int n1, int n2) string double_to_str(double v) { char buf[256]; - snprintf(buf, sizeof(buf), "%.2f", v); + double rounded_v = round(v * 100.0) / 100.0; + snprintf(buf, sizeof(buf), "%.2f", rounded_v); size_t len = strlen(buf); while (buf[len - 1] == '0') { len--; diff --git a/docs/docs/game/miniob-output-convention.md b/docs/docs/game/miniob-output-convention.md index 15999d0c8..96d165fcf 100644 --- a/docs/docs/game/miniob-output-convention.md +++ b/docs/docs/game/miniob-output-convention.md @@ -28,7 +28,7 @@ NOTE:后台测试程序,是将预先编辑好的Case执行后,将执行结 4. 所有输出不区分大小写 5. 日期(date)输出格式使用:"YYYY-mm-DD" 6. 输出的字符串不使用单引号双引号或其它括起来 -7. 浮点数输出,不要带后面多余的0,可以参考C sprintf的%g格式输出,保留两位小数。参考函数 double_to_str +7. 浮点数输出,不要带后面多余的0,四舍五入保留两位小数。参考函数 double_to_str FAQ - 某张表或者某个查询结果一行数据都没有,但是依然需要输出表头信息