Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print trailing zeros in a QString

I am using Qt and want to print a data value (double) in a label; however, the trailing zeros are lopped off. I know in C I can use printf("%0.1f", data) to preserve the trailing zeros.

I looked at QString's arg function but that only allows the overall field width to be set. setNum and number each allow the precision to be set but that's not right either.

Example code:

double data = 1.0;
label->setText( QString().number( data );
like image 611
dwj Avatar asked May 19 '09 22:05

dwj


2 Answers

Look at the static function QString::number() with format and precision arguments.

QString QString::number( double n, char format = 'g', int precision = 6 )

Reference: http://doc.qtsoftware.com/4.5/qstring.html#number-2

like image 197
swongu Avatar answered Nov 18 '22 16:11

swongu


Why not use QString::sprintf() ?

QString().sprintf("%08d + rest of the string", 7);
like image 3
François Avatar answered Nov 18 '22 17:11

François