[Qt-interest] Cutting useless decimal places

Andre Somers andre at familiesomers.nl
Mon Oct 25 12:09:31 CEST 2010


Hi,

Op Ma, 25 oktober, 2010 10:03 am, schreef mailinglists at bluespirit.la:
> Hello,
>
> a very simple question: How can I create a QString from a
> float/double, in format 'f' without giving a explicit number of decimal
> places to the arg() arguments?
>
> qDebug() << QString("Hello world: %1").arg( fNumber, 0, 'f' );
>
> When setting fNumbers to '0.125' I want an output of '0.125'. But when
> setting the fNumbers variable to '200', I don't want an output of
> "200.000". I want also use the localized output - so I prefer
> QString::arg() to QString::sprintf(). The use of precision==-1 is not
> documented - at least not in qt 4.6 api-doc.
>
> Thank in advance,

What kind of output are you looking for *exactly*? Do you want to get
output to the a pre-defined number of significant digits? What should be
the output if your number is 2001? Or 200001? And 0.00200001? Did you take
into account that floating point numbers are often not precise?

I once wrote a function that returns "2.00 thousand" and "200 thousand"
for the first two cases (it does not currently support numbers < 1) so you
get some reasonable precision that is fit for human consumption. But I am
not sure that is what you are after.

André






More information about the Qt-interest-old mailing list