[Qt-interest] number formatting in output textstream

Sean Harmer sean.harmer at esrtechnology.com
Tue Oct 6 09:42:58 CEST 2009


Hi,

> I am trying to output data to a textfile. The file contains both integers
> and floating point numbers. Due to the program reading in the data, I have
> specific requirements to the formatting of these numbers. Specifically, I
> need the integers be formatted without anything added (just the number, no
> thousands separators) but the floats formatted as defined in the locale
> (here usually a comma is used for the decimals).
>
>
>
> I tried the following approach:
>
>
>
> QTextStream s(&file); // file is a QFile, opened in writing mode
>
> QLocale l = QLocale::system();  //get system locale
>
> l.setNumberOptions(QLocale::OmitGroupSeparator); // I don’t want to use
> separators, but are ‘group-separators’ the same as thousands-separators?
>
> s.setLocale(l);
>
>
>
> s << integer1 << “ “ << integer2 << “ “ << float1 << endl; // output a
> line
> of data
Try this:

s << QString::number( integer1 ) << QString::number( integer2 ) << float1
<< endl;

That way QTextStream just sees a string for the ints rather than an int so
it will do no conversion.

HTH,

Sean





More information about the Qt-interest-old mailing list