将数字转换为填充字符串

Converting number to a padded string

本文关键字:填充 字符串 转换 数字      更新时间:2023-10-16

我正在尝试将数字类型转换为带有前导'0'的固定长度字符串表示形式(用于只能处理字符串的GUI - 数字的词典排序真的很尴尬(。

我认为应该可以制作一个不错的模板供其他人使用,而不是在需要的地方创建字符串,该模板也适用于不同的算术类型。

template <typename T, typename std::enable_if<std::is_arithmetic<T>::value, T>::type* = nullptr>
std::string arithmeticToMaxLengthString(const T& value)
{
std::string number, result;
number = std::to_string( value );
result = lpad( '0', std::numeric_limits<decltype(value)>::digits10 + 1, number );
return result;
}
std::string lpad( char filler, std::string::size_type length, const std::string& input)
{
std::string result;
if( input.length() < length )
{
result.append( lenght - input.lenght(), filler ).append( input );
return result;
}
return input;
}

目前为止,一切都好。 它可以编译,我可以使用它。 但:

当我尝试使用它时,我得到了一个惊喜。 使用int32_t调用此函数,该

typedef int int32_t;

值为 100的结果是字符串"100",长度为 3。

我很确定代码库中不存在其他名称如此糟糕的模板(我已经搜索了所有这些模板以及所有链接库(,但我似乎无法理解我生成的代码有什么问题。 为了验证代码是否正确,我只是将其复制粘贴到调用它的位置,现在基本上看起来像这样:

myDataClass{
int32_t     quantity;
std::string numberAsString;
}
void setNumberString( myDataClass data )
{
std::string result    = arithmeticToMaxLengthString( data.quantity );
std::string reference = lpad( '0', std::numeric_limits<decltype(value)>::digits10 + 1, std::to_string(data.quantity) );
std::cout << "quantity<" << data.quantity << "> numericLimits<" << std::numeric_limits<decltype(value)>::digits10 + 1 << ">" << std::endl;
std::cout << "result  template: length<" << result.lenght()    << "> content<" << result    << ">" << std::endl;
std::cout << "result reference: length<" << reference.lenght() << "> content<" << reference << ">" << std::endl;
}

现在据我了解,模板"arithmeticToMaxLengthString"以int32_t作为参数的类型调用,这是一个普通的整数,其中"numeric_limits::d igits10 + 1"给了我10。 但是,当现在调用此功能时,数量为 100,结果会有所不同。

quantity<100> numericLimits<10>
result  template: length<3> content<100>
result reference: length<10> content<0000000100>

我是否缺少有关模板、enable_if或numeric_limits的内容? 模板在哪些方面有所不同(除了明显的enable_if(?

发生此问题是因为您正在传递对arithmeticToMaxLengthString函数的引用。由于此处另一个答案中列出的原因,std::numeric_limits不适用于参考文献。

因此,我建议您使用模板类型T来获取数值限制,而不是使用decltype(value)

result = lpad('0', std::numeric_limits<T>::digits10 + 1, number);

它不仅解决了问题,而且看起来也更加优雅。

这不是您问题的答案,而是如何使用STL实现相同功能的建议。

#include <iomanip>
#include <sstream>
template <typename T, unsigned N = 5>
std::string to_string_with_leading_0(T element) {
std::stringstream ss;
ss << std::setw(N) << std::setfill('0') << element;
return ss.str();
}