将 int 转换为带有零填充(前导零)的 QString

发布于 2024-08-28 15:21:27 字数 75 浏览 4 评论 0原文

我想要“字符串化”一个数字并添加零填充,就像如果数字少于 5 位,printf("%05d") 将添加前导零。

I want to "stringify" a number and add zero-padding, like how printf("%05d") would add leading zeros if the number is less than 5 digits.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

内心激荡 2024-09-04 15:21:27

使用这个:

QString number = QStringLiteral("%1").arg(yourNumber, 5, 10, QLatin1Char('0'));

这里的 5 对应于 printf("%05d") 中的 5。 10是基数,你可以输入16来打印十六进制的数字。

Use this:

QString number = QStringLiteral("%1").arg(yourNumber, 5, 10, QLatin1Char('0'));

5 here corresponds to 5 in printf("%05d"). 10 is the radix, you can put 16 to print the number in hex.

把时间冻结 2024-09-04 15:21:27

QString QString::rightJustified ( int width, QChar fill = QLatin1Char( ' ' ), bool truncate = false ) const

int myNumber = 99;
QString result;
result = QString::number(myNumber).rightJustified(5, '0');

结果现在是 00099

QString QString::rightJustified ( int width, QChar fill = QLatin1Char( ' ' ), bool truncate = false ) const

int myNumber = 99;
QString result;
result = QString::number(myNumber).rightJustified(5, '0');

result is now 00099

七度光 2024-09-04 15:21:27

简短示例:

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

The Short Example:

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
末蓝 2024-09-04 15:21:27

尝试:

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"

阅读其他文档以了解此方法缺少的功能。

Try:

QString s = s.sprintf("%08X",yournumber);

EDIT:
According to the docs at http://qt-project.org/doc/qt-4.8/qstring.html#sprintf:

Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or arg(), both of which support Unicode strings seamlessly and are type-safe. Here's an example that uses QTextStream:

QString result;
QTextStream(&result) << "pi = " << 3.14;
// result == "pi = 3.14"

Read the other docs for features missing from this method.

爱格式化 2024-09-04 15:21:27

我正在尝试这个(确实有效,但很麻烦)。

QString s;
s.setNum(n,base);
s = s.toUpper();
presision -= s.length();
while(presision>0){
    s.prepend('0');
    presision--;
}

I was trying this (which does work, but cumbersome).

QString s;
s.setNum(n,base);
s = s.toUpper();
presision -= s.length();
while(presision>0){
    s.prepend('0');
    presision--;
}
猛虎独行 2024-09-04 15:21:27

我从 VB 5 开始就使用一种技术

QString theStr=QString("0000%1").arg(theNumber).right(4);

I use a technique since VB 5

QString theStr=QString("0000%1").arg(theNumber).right(4);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文