C/C++ 将 double 转换为具有最少非零数字位数的定点数
C/C++ conversion of double to a fixed point with minimum number of non-zero digits
多个定点十进制数映射到同一个双精度值。
例如:
double d1 = 132.24;
double d2 = 132.24000000000001;
d1 和 d2 具有相同的二进制值。
使用14位精度的ostream将d1(或d2,它们具有相同的值)转换为字符串时,转换为:132.24000000000001。
是否有支持从double->string转换的way/library,其中string是定点值,非零数字最少那是一个有效的转换?即在这种情况下将 d1 转换为字符串将 return 132.24000000000000(以 14 位精度显示)
图书馆https://github.com/floitschG/double-conversion
提供带有 SHORTEST 选项的 DoubleToStringConverter::DoubleToAscii 方法:
"SHORTEST: produce the least amount of digits for which the internal identity requirement is still satisfied."
非常感谢 iwillnotexist-idonotexist 为我指明了正确的方向。
多个定点十进制数映射到同一个双精度值。 例如:
double d1 = 132.24;
double d2 = 132.24000000000001;
d1 和 d2 具有相同的二进制值。
使用14位精度的ostream将d1(或d2,它们具有相同的值)转换为字符串时,转换为:132.24000000000001。
是否有支持从double->string转换的way/library,其中string是定点值,非零数字最少那是一个有效的转换?即在这种情况下将 d1 转换为字符串将 return 132.24000000000000(以 14 位精度显示)
图书馆https://github.com/floitschG/double-conversion 提供带有 SHORTEST 选项的 DoubleToStringConverter::DoubleToAscii 方法: "SHORTEST: produce the least amount of digits for which the internal identity requirement is still satisfied."
非常感谢 iwillnotexist-idonotexist 为我指明了正确的方向。