Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. String format to 2 digit zero padding numbers
QtWS25 Last Chance

String format to 2 digit zero padding numbers

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pixbyte
    wrote on 28 Jan 2020, 09:02 last edited by
    #1

    I have a question about string format. I need to format a date to a String with zero padding.
    Like 2020.01.27

    I use

    QString strCreationTime = QString("%1.%2.%3 %4:%5:%6").arg(QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nDay), 
    QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nMonth),
    QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nYear+1900),
    QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nHour), QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nMinute),
    QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nSecond));
    

    How is it possible to bring one digit numbers to 2 digit zero padding? Something like sprintf possible with QString?

    J 1 Reply Last reply 28 Jan 2020, 09:17
    0
    • J J.Hilk
      28 Jan 2020, 09:17

      @pixbyte please read the documentation
      https://doc.qt.io/qt-5/qstring.html

      arg has multiple overloads, more than one allows you to add 0 padding

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 28 Jan 2020, 09:53 last edited by aha_1980
      #4

      As @J-Hilk said, there are overloads like this:

      QString number = QString("%1.%2").arg(number, 2, 10, QLatin1Char('0')).arg(number2, 2, 10, QLatin1Char('0');

      But it may be easier if you convert your ISOCreationDateTime into QDateTime and use the string formating from there?

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      2
      • P pixbyte
        28 Jan 2020, 09:02

        I have a question about string format. I need to format a date to a String with zero padding.
        Like 2020.01.27

        I use

        QString strCreationTime = QString("%1.%2.%3 %4:%5:%6").arg(QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nDay), 
        QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nMonth),
        QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nYear+1900),
        QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nHour), QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nMinute),
        QString::number(pISOVolumeInfo.sInfoEx.ISOCreationDateTime.nSecond));
        

        How is it possible to bring one digit numbers to 2 digit zero padding? Something like sprintf possible with QString?

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 28 Jan 2020, 09:17 last edited by
        #2

        @pixbyte please read the documentation
        https://doc.qt.io/qt-5/qstring.html

        arg has multiple overloads, more than one allows you to add 0 padding


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        A 1 Reply Last reply 28 Jan 2020, 09:53
        1
        • P Offline
          P Offline
          pixbyte
          wrote on 28 Jan 2020, 09:51 last edited by
          #3

          You are right but I so not know how to use with this multiple formating %1%2%3, for one, just like %1 it is easy.

          1 Reply Last reply
          0
          • J J.Hilk
            28 Jan 2020, 09:17

            @pixbyte please read the documentation
            https://doc.qt.io/qt-5/qstring.html

            arg has multiple overloads, more than one allows you to add 0 padding

            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 28 Jan 2020, 09:53 last edited by aha_1980
            #4

            As @J-Hilk said, there are overloads like this:

            QString number = QString("%1.%2").arg(number, 2, 10, QLatin1Char('0')).arg(number2, 2, 10, QLatin1Char('0');

            But it may be easier if you convert your ISOCreationDateTime into QDateTime and use the string formating from there?

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply
            2

            3/4

            28 Jan 2020, 09:51

            • Login

            • Login or register to search.
            3 out of 4
            • First post
              3/4
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved