Convert an int to a QString with zero padding (leading zeroes)

103,830

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Share:
103,830

Related videos on Youtube

elcuco
Author by

elcuco

SOreadytohelp, yea, I don't like Reggae (I love it). But not twitter.

Updated on July 11, 2020

Comments

  • elcuco
    elcuco almost 4 years

    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.

  • elcuco
    elcuco over 12 years
    nice, not marking as the best fix, as the currently marked answer is more flexible. But nice to know! (upvoting)
  • migas
    migas about 10 years
    Thanks. Documentation here.
  • elcuco
    elcuco over 8 years
    A lot of memcpy() and a log of wasted memory/cpu. If you are using Qt/C++ you want performance (that said, I saw this trick using in JS, and it's a good hack on that platform).
  • Michal
    Michal over 8 years
    Very useful for creating hex color codes from integer, thanks :)
  • dhein
    dhein almost 8 years
    And why the string argument is "%1"?
  • Paul Masri-Stone
    Paul Masri-Stone over 7 years
    @Zaibis Because it's the first argument. If there were more, they would be %2 etc. See doc.qt.io/qt-5/qstring.html#arg
  • Lennart Rolland
    Lennart Rolland about 7 years
    Don't give up!!
  • Rudy Barbieri
    Rudy Barbieri over 6 years
    thank you, I'm creating a string on the fly using += so the selected solution couldn't be used.
  • Jimit Rupani
    Jimit Rupani about 6 years
    @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.
  • cleybertandre
    cleybertandre almost 4 years
    Correspondent with QByteArray: QByteArray::number(yourNumber, 10).rightJustified(5, '0');
  • Trass3r
    Trass3r almost 4 years
    There should be a more efficient way to do this.
  • Trass3r
    Trass3r almost 4 years
    Better but still creates a temporary string :/