views:

837

answers:

1

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 );
+3  A: 

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

swongu
I overlooked changing the format from the default 'g' to 'f' since 'g' includes 'f'. Missed some words in the description of 'f' as well.Correct answer: double data = 1.0; label->setText( QString().number( data, 'f', 1 );
dwj