在转换浮点值时设置std::to_string的精度
Set precision of std::to_string when converting floating point values
在c++ 11中,当输入类型为float
或double
时,std::to_string
默认为小数点后6位。推荐的或者最优雅的改变精度的方法是什么?
无法通过to_string()
改变精度,但可以使用setprecision
IO操纵器:
#include <sstream>
template <typename T>
std::string to_string_with_precision(const T a_value, const int n = 6)
{
std::ostringstream out;
out.precision(n);
out << std::fixed << a_value;
return std::move(out).str();
}
相关文章:
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- std::time_point from and to std::string
- "No suitable conversion function from 'std::string' to 'const char *' exists"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 使用词法强制转换在'string to double'中设置双精度变量的精度
- 在 Lua 中加载 C++ 模块时'Attempt to index a string value'错误
- std::string to std::regex
- "How to pass String value in between URL in C/C++?"
- "conversion from void to non scalar type string"是什么意思?
- 我收到一个" conversion from ‘std::string (*)[50]’ to non-scalar type ‘std::string’ requested"错误,似乎无法修复它
- 如何防止从函数生成的字符串中使用科学记数法"number to string"?
- hwProfileInfo.szHwProfileGuid to String?
- 编写我自己的字符串类"reference to ‘string’ is ambiguous"但出现编译错误
- c++ stringstream to ostream to string
- How cin to string?
- C++ *string convert to string
- Round Double and Cast to String
- Regex-ed value to String
- vector string to string (or c_string) to lpszString