Function that use setfill and setw to return a QString
Solved
General and Desktop
-
Hi guys, i'm trying to write a function that gets two int and sets a particular String with a certain number of leading 0s.
Something like this:int a=5; int b=21; void MainWindow::Function(int a,int b){ // String s= std::setfill('0') << std::setw(a) << b ; // qDebug()<<"value of s -> "<<s; // Expected result -> 00021 ; //if a=3 i would expect -> 021; }
So i can do something like :
int z=35; Function(4,z); // set String s='0035' qDebug()<<"s value -> "<<s // "s value -> 0035"
Any kind of help would be appreciated
thanks -
QStringLiteral("%1").arg(b, a, 10, QChar('0'))
or
QString::number(b).rightJustified(a, '0')
-
@Chris-Kawa Thank you very much.