C++11格式化整数前面补0和格式化小数小数补0

xingyun86 13小时前 9

C++11格式化整数前面补0和格式化小数小数补0

#include <iomanip>
template <typename T>
std::wstring to_wstring_precision(const T v, const int n = 6)
{
	std::wostringstream out;
	out << std::fixed << std::setprecision(n) << v;
	return out.str();
}
template <typename T>
std::wstring to_wstring_filezero(const T v, const int n = 6)
{
	std::wstringstream out;
	out << std::setw(n) << std::setfill(L'0') << v;
	return out.str();
}

用法:

std::wstring ws = to_wstring_precision(1.5, 3);//固定3位小数格式化,不足则小数尾部补0

std::wstring ws = to_wstring_filezero(1, 3);//固定3个字符格式化,不足前面补0

×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回