cstring.format具有可变浮点精度
CString.Format with variable float precision
我有一个float值:( data->val
),它可能是三个可能的浮点精度: %.1f
, %.2f
and %.3f
我如何使用 CString::Format
将其格式化为只需dsiplay所需的小数次数?例如:
CString sVal;
sVal.Format(L"%.<WHAT GOES HERE?>f", data->val);
if(stValue)
stValue->SetWindowText(sVal);
我不希望在我的格式字符串的末端任何其他零。
如果您知道只需使用%.*f
并将精度作为整数参数提供到CString::Format
。如果您需要最简单的有效表示,请尝试%g
:
int precision = 2; // whatever you figure the precision to be
sVal.Format(L"%.*f", precision, data->val);
// likely better: sVal.Format(L"%g", data->val);
它前一次,但这也许会起作用...
CString getPrecisionString(int len)
{
CString result;
result.format( "%s%d%s","%.", len, "f" );
return result;
}
// somewhere else
CString sVal;
sVal.Format(getPrecisionString(2), data->val);
if(stValue)
stValue->SetWindowText(sVal);
另一种方法是在添加%.3F值
之后剪切0是sVal.trimEnd('0')
,但很危险,因为您可能拥有'。最后...
相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 如何计算具有指定类型的表达式的相对精度和绝对精度
- 如何打印boost多精度128位无符号整数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 为什么输出精度没有正确舍入?
- 在 C++ 中将双精度变量写入二进制文件
- cstring.format具有可变浮点精度