Replacing QString().sprintf with arg calls for double
-
Hello,
I am trying to update my code from using sprintf to arg, as sprintf is deprecated and asprintf is not advised for new code.
But I am getting some issues with converting doubles to strings. With arg you can specify precision and a fillchar, but with doubles this fillchar is also applied to the frontThis is my example code:
int main(int argc, char *argv[]) { QTextStream output(stdout); output << QString().sprintf("%12.6lf", 123.456) << endl; output << QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0')) << endl; return 0; }
This results in the following output:
123.456000 00123.456000
Is it possible to change this behaviour of QString(..).arg(..)?
Regards,
Johan -
Hello,
I am trying to update my code from using sprintf to arg, as sprintf is deprecated and asprintf is not advised for new code.
But I am getting some issues with converting doubles to strings. With arg you can specify precision and a fillchar, but with doubles this fillchar is also applied to the frontThis is my example code:
int main(int argc, char *argv[]) { QTextStream output(stdout); output << QString().sprintf("%12.6lf", 123.456) << endl; output << QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0')) << endl; return 0; }
This results in the following output:
123.456000 00123.456000
Is it possible to change this behaviour of QString(..).arg(..)?
Regards,
JohanqDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char(' ')); qDebug()<<QString::asprintf("% 12.6lf", 123.456); " 123.456000" " 123.456000"
or
qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0')); qDebug()<<QString::asprintf("%012.6lf", 123.456); "00123.456000" "00123.456000"
-
Hello,
I am trying to update my code from using sprintf to arg, as sprintf is deprecated and asprintf is not advised for new code.
But I am getting some issues with converting doubles to strings. With arg you can specify precision and a fillchar, but with doubles this fillchar is also applied to the frontThis is my example code:
int main(int argc, char *argv[]) { QTextStream output(stdout); output << QString().sprintf("%12.6lf", 123.456) << endl; output << QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0')) << endl; return 0; }
This results in the following output:
123.456000 00123.456000
Is it possible to change this behaviour of QString(..).arg(..)?
Regards,
JohanThis post is deleted! -
qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char(' ')); qDebug()<<QString::asprintf("% 12.6lf", 123.456); " 123.456000" " 123.456000"
or
qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char('0')); qDebug()<<QString::asprintf("%012.6lf", 123.456); "00123.456000" "00123.456000"
@mpergand said in Replacing QString().sprintf with arg calls for double:
qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char(' '));
This is what the OP wants. I believe [can't test]
u' '
is the default so can presumably be omitted:QString("%1").arg(123.456, 12, 'f', 6);
?
-
@mpergand said in Replacing QString().sprintf with arg calls for double:
qDebug()<<QString("%1").arg(123.456, 12, 'f', 6, QLatin1Char(' '));
This is what the OP wants. I believe [can't test]
u' '
is the default so can presumably be omitted:QString("%1").arg(123.456, 12, 'f', 6);
?
-
@JonB said in Replacing QString().sprintf with arg calls for double:
' ' is the default so can presumably be omitted:
Yes but "ça va mieux en le disant" :)
-
Thank you all for this. I was expecting that I needed the '0' fillchar to fill at the end as well, but I was clearly wrong there. This helped me a lot.
-