[Qt-interest] QString::number question

Arnold Krille arnold at arnoldarts.de
Wed May 26 22:05:10 CEST 2010


Hi,

On Wednesday 26 May 2010 20:39:41 Marc Ferland wrote:
> I'm currently using the QString::number() function to convert real
> values to strings.  More precisely I'm using the functio with these
> arguments: QString::number(num, 'g', 4);
> 
> The result is a little surprising when the number is near 0. Here's a
> little program that shows the problem I'm having:
> 
> #include <QString>
> #include <QtGlobal>
> #include <QtDebug>
> 
> int main()
> {
>    quint32 i;
>    qreal step = 0.2;
>    qreal start = -1.0;
> 
>    for (i = 0; i < 10; ++i)
>    {
>       qDebug() << "QString::number(start, 'g', 4)" <<
> QString(QString::number(start, 'g', 4)); qDebug() <<
> "QString::number(start, 'f', 4)" << QString(QString::number(start, 'f',
> 4)); start += step;
>    }
> }
> 
> The output is:
> QString::number(start, 'g', 4) "-1"
> QString::number(start, 'f', 4) "-1.0000"
> QString::number(start, 'g', 4) "-0.8"
> QString::number(start, 'f', 4) "-0.8000"
> QString::number(start, 'g', 4) "-0.6"
> QString::number(start, 'f', 4) "-0.6000"
> QString::number(start, 'g', 4) "-0.4"
> QString::number(start, 'f', 4) "-0.4000"
> QString::number(start, 'g', 4) "-0.2"
> QString::number(start, 'f', 4) "-0.2000"
> QString::number(start, 'g', 4) "-5.551e-17"
> QString::number(start, 'f', 4) "-0.0000"
> QString::number(start, 'g', 4) "0.2"
> QString::number(start, 'f', 4) "0.2000"
> QString::number(start, 'g', 4) "0.4"
> QString::number(start, 'f', 4) "0.4000"
> QString::number(start, 'g', 4) "0.6"
> QString::number(start, 'f', 4) "0.6000"
> QString::number(start, 'g', 4) "0.8"
> QString::number(start, 'f', 4) "0.8000"
> 
> All the converted numbers seem ok, except for the 0 which shows as
> "-0.000" or "-5.551e-17". Is there anyway to tell QString to just
> round to the nearest value which makes sense (based on the precision
> for example)?
> 
> Am I forced to use a small epsilon to detect when the value is near
> zero?

You get what you should expect!
With 'f' you get a number with a given number of digits after the colon. With 
'e' you get a number with a given number of significant digits. That is 
actually one of the advantages of using 'e': It gives you the significant part 
of the number. If you don't want that, don't use it :-)

What does 'e' have to do with 'g'? 'g' is 'f' or 'e' depending on what makes 
more sense. That is if the number can be written with 'f' inside the given 
number of digits, its used, otherwise 'e' is used...

That is probably not the answer you where looking for, but thats how it is.

If you want small numbers to be 0.0, you have to work with an epsilon.
But 1e-17 is not always a small number:-)

Have fun,

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100526/7206963a/attachment.bin 


More information about the Qt-interest-old mailing list