15

I want to convert number to QString with 3 significant digits.

QString::number(myNumber,'f',3);

does the job but remains trailing zeros. How to use it without them.

Also I've tried 'g' and which shouldn't remain those zeros:

QString::number(myNumber,'g',3);

but for example 472.76 is converted to 473. That surprised me. Why is it so with 'g' option?

However I am interested in 'f' format. So the main question is how to do it with 'f' without trailing zeros?

Input -> Desired output

472.76 -> 472.76

0.0766861 -> 0.077

180.00001 -> 180

krzych
  • 2,126
  • 7
  • 31
  • 50

5 Answers5

15

I'm almost embarrassed to post this but it works:

QString toString( qreal num )
{
    QString str = QString::number( num, 'f', 3 );

    str.remove( QRegExp("0+$") ); // Remove any number of trailing 0's
    str.remove( QRegExp("\\.$") ); // If the last character is just a '.' then remove it

    return str;
}

If you're really concerned about the performance using this method you may want to come up with a different solution.

Chris
  • 17,119
  • 5
  • 57
  • 60
4
QString::number(myNumber,'g',3);

Will limit the conversion to the significant digits. You'll most always have 3 numbers.

472.76 => 472
4.7276 => 4.72

Try using the formatted printing functions like QString::sprintf. This should give you more control.

QString numStr;
numStr.sprintf("f.3f", myNumber);
Aesthete
  • 18,622
  • 6
  • 36
  • 45
1

If you insist to have the precision after the decimal point you have to use 'f'. Here is an option to remove the trailing zeors with better performance than the Regex using only QString builtins:

QString str = QString::number(myNumber,'f',3);
while(str.back() =='0')
{
    str.chop(1);
}
if(str.back() =='.')
{
    str.chop(1);
}

This works because the f option guarantees to put out the defined digits. It's about 50% faster than C sprintf and 40% faster than QString::sprintf from the other answer.

Moritz H.
  • 21
  • 5
0
QString str;
str.setNum(myNumber, 'f', 3);

This will do what you need, I tested it. Strangely, "number" behaves differently than "setNum".

bmkorkut
  • 616
  • 5
  • 17
  • 4
    It remains zeros. For example 472.76000 is converted to 472.760 and should be 472.76. Works for me the same as number. – krzych Sep 17 '12 at 13:34
0

How about this. It is maybe more performant than the variant with regular expressions. The mynumber() function takes a new format char 'h', which does the job.

QString mynumber(qreal p_number, char p_format, int p_precision)
{
    if(p_format=='h')
    {
        //like 'f' but removes trailing zeros and the '.' evtl
        QString res=QString::number(p_number,'f',p_precision);
        int countTrailingToRemove=0;
        for(QString::const_reverse_iterator it=res.crbegin();it!=res.crend();it++)
        {
            if(*it=='0') countTrailingToRemove++;
            else
            {
                if(*it=='.') countTrailingToRemove++;
                break;
            }
        }

        return res.mid(0,res.length()-countTrailingToRemove);
    }
    return QString::number(p_number,p_format,p_precision);

}
gordon
  • 1