String format to 2 digit zero padding numbers
-
I have a question about string format. I need to format a date to a String with zero padding.
Like 2020.01.27I 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?
-
@pixbyte please read the documentation
https://doc.qt.io/qt-5/qstring.htmlarg has multiple overloads, more than one allows you to add 0 padding
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
-
I have a question about string format. I need to format a date to a String with zero padding.
Like 2020.01.27I 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?
@pixbyte please read the documentation
https://doc.qt.io/qt-5/qstring.htmlarg has multiple overloads, more than one allows you to add 0 padding
-
@pixbyte please read the documentation
https://doc.qt.io/qt-5/qstring.htmlarg has multiple overloads, more than one allows you to add 0 padding
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