[Qt-interest] number formatting in output textstream
Andre Somers
andre at familiesomers.nl
Tue Oct 6 10:41:43 CEST 2009
Sean Harmer wrote:
> 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.
>
It does, thank you! I should have thought of this workaround myself.
I am still wondering what the QLocale::OmitGroupSeparator flag is
supposed to do, if not suppres thousands separators. Can anyone clarify?
André
More information about the Qt-interest-old
mailing list