题
我想“字符串”的数,并添加补零,怎么样printf("%05d")
将添加前导零,如果数目小于5个数字。
解决方案
使用这样的:
QString number = QString("%1").arg(yourNumber, 5, 10, QChar('0'));
5在此对应于5 printf("%05d")
。 10是基数,就可以把16打印在十六进制数。
其他提示
的QString的QString :: rightJustified(INT宽度,QChar则填充= QLatin1Char( ''),布尔截=假)const的
int myNumber = 99;
QString result;
result = QString::number(myNumber).rightJustified(5, '0');
结果是现在00099
<强>短示例:强>
int myNumber = 9;
//Arg1: the number
//Arg2: how many 0 you want?
//Arg3: The base (10 - decimal, 16 hexadecimal - if you don't understand, choose 10)
// It seems like only decimal can support negative numbers.
QString number = QString("%1").arg(myNumber, 2, 10, QChar('0'));
Output will be: 09
尝试:
QString s = s.sprintf("%08X",yournumber);
编辑: 根据该文档在 http://qt-project.org/doc/ QT-4.8 / qstring.html#的sprintf :
警告:我们不建议在新的Qt代码使用的QString :: sprintf的()。相反,考虑使用QTextStream或ARG(),这两者都支持Unicode字符串无缝且是类型安全的。下面是一个使用QTextStream的示例:
QString result;
QTextStream(&result) << "pi = " << 3.14;
// result == "pi = 3.14"
读取其他文档为特征从该方法丢失。
我试图本(其工作,但笨重)。
QString s;
s.setNum(n,base);
s = s.toUpper();
presision -= s.length();
while(presision>0){
s.prepend('0');
presision--;
}
我用的技术,因为VB 5
QString theStr=QString("0000%1").arg(theNumber).right(4);
function zeroPad(num, places) {
num = num.toString().toUpperCase();
return num.length < places ? zeroPad("0" + num, places) : num;
}
我用这个递归函数来垫零!
不隶属于 StackOverflow