[Interest] QTextStream: output representation of floating point number with exactly n digits

d3fault d3faultdotxbe at gmail.com
Sun Oct 7 12:03:32 CEST 2012


On Fri, Oct 5, 2012 at 4:28 PM, Alex Malyushytskyy <alexmalvtk at gmail.com> wrote:
> setRealNumberNotation ( QTextStream::ScientificNotation )
> with  setRealNumberPrecision() to specify number of digits?
>

I haven't tried the QTextStream::setRealNumber* methods, but you
probably want to pass in QTextStream::FixedNotation instead of
QTextStream::ScientificNotation to setRealNumberNotation if you don't
want random 'E's to show up in the resulting text (see the last link).

Another way you could do it is to convert the double/float to QString
before passing it into QTextStream:
textStream << QString::number(doubleOrFloat, 'f', 8); //will convert
'doubleOrFloat' to QString with 8 points of precision (including
trailing zeros) before passing it to textStream.
Using the QTextStream::setRealNumber* methods probably does the same
thing behind the scenes.

Have a look at:
http://qt-project.org/doc/qt-4.8/qtextstream.html#RealNumberNotation-enum
http://qt-project.org/doc/qt-4.8/qstring.html#number-2
http://qt-project.org/doc/qt-4.8/qstring.html#argument-formats

d3fault

>
> Alex
>
> On Fri, Oct 5, 2012 at 3:59 PM, Rui Maciel <rui.maciel at gmail.com> wrote:
>> Is it possible to manipulate QTextStream so that it outputs a
>> representation of a float or a double with exactly n digits?  I've tried
>> setRealNumberPrecision() but it doesn't appear to have any effect.
>>
>>
>> Thanks in advance,
>> Rui Maciel
>>



More information about the Interest mailing list