112

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
elcuco
  • 8,948
  • 9
  • 47
  • 69

6 Answers6

206

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.

Trass3r
  • 5,858
  • 2
  • 30
  • 45
chalup
  • 8,358
  • 3
  • 33
  • 38
  • 8
    Thanks. Documentation [here](http://qt-project.org/doc/qt-4.8/qstring.html#arg-10). – migas Mar 18 '14 at 11:02
  • 2
    Very useful for creating hex color codes from integer, thanks :) – Michal Aug 21 '15 at 11:07
  • And why the string argument is `"%1"`? – dhein Jun 08 '16 at 14:05
  • @Zaibis Because it's the first argument. If there were more, they would be `%2` etc. See http://doc.qt.io/qt-5/qstring.html#arg – Paul Masri-Stone Oct 06 '16 at 18:10
  • @sashoalm how we can remove the same number of leading zero? ex. My hex was 1f with QString("%1").arg(yourNumber, 5, 10, QChar('0')); it became 001f. now how to do the reverse to get 1f again? thanks for the help in an advance. – Jimit Rupani Feb 15 '18 at 13:53
  • Correspondent with QByteArray: QByteArray::number(yourNumber, 10).rightJustified(5, '0'); – cleybertandre May 05 '20 at 13:42
  • There should be a more efficient way to do this. – Trass3r Jul 10 '20 at 12:11
  • Not sure if there's a more efficient way but you could do `QString::number(padded.toInt(), 16)` where `padded` is your string with leading zeros – Ege F Sep 26 '22 at 17:09
58

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

20

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
elcuco
  • 8,948
  • 9
  • 47
  • 69
user1767754
  • 23,311
  • 18
  • 141
  • 164
5

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.

elcuco
  • 8,948
  • 9
  • 47
  • 69
Isaac
  • 51
  • 1
  • 1
  • nice, not marking as the best fix, as the currently marked answer is more flexible. But nice to know! (upvoting) – elcuco Sep 12 '11 at 19:37
-1

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--;
}
elcuco
  • 8,948
  • 9
  • 47
  • 69
-1

I use a technique since VB 5

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