-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
自定义结构体转jsonStr的时候 打印出来的浮点值会有科学计数法的后缀E0 #255
Comments
这样问题不大吧,能正确反序列化就行。 |
能否加一个控制浮点数有效数字的接口? 同时能够配置是否按照科学计数法显示? |
我们需要把序列化的浮点结果写到日志里面,需要控制打印的位数 |
打算提供一个接口,让用户可以自定义使用浮点数序列化,今天晚一点提PR |
重载to_chars_float, 看上去编译完后buffer的位数已经确定, 后续有没有可能支持动态指定的方式,类似
或者 其他能够基于当前代码实现的方案? |
那个buffer并不是实际的位数,只是最大容量,64位double + 一个结束字符 = 65。 int floatPrecision = 5;
int doublePrecision = 10;
template <typename T>
inline char* to_chars_float(T value, char* buffer) {
static_assert(std::is_floating_point<T>::value, "floating-point type needed");
char format[16];
if (std::is_same<T, float>::value) {
std::snprintf(format, sizeof(format), "%%.%dg", floatPrecision);
} else {
std::snprintf(format, sizeof(format), "%%.%dg", doublePrecision);
}
int length = std::snprintf(buffer, 65, format, value);
if (length == 0) {
// throw
}
return buffer + length;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
版本:v1.0.3
程序内部是:
double test = 2.011111;
iguana::to_json后打印出来:
{ "test": 2.011111E0}
The text was updated successfully, but these errors were encountered: