QDomDocument always uses system locale settings while writing double numbers in Linux.
-
I have tried to write down float and double numbers to xml file. But there is a problem:
float numbers always writing with dot decimal separator and double numbers wtiteing with comma seperator even if programm locale set to QLocale::C.Thera two variables:
float floatVal = 0.1234f;
double doubleVal = 0.1234;When I wtite them to QDomDocument the output file looks like this:
<Root doubleVal="0,1234" floatVal="0.1234"/>
This issue only happens in Linux. In Windows everythins is Ok.
Is it a bug?System locale settings are:
user@paefimov:~$ locale LANG=en_US.UTF-8 LANGUAGE=en_US LC_CTYPE="en_US.UTF-8" LC_NUMERIC=ru_RU.UTF-8 LC_TIME=en_US.UTF-8 LC_COLLATE="en_US.UTF-8" LC_MONETARY=ru_RU.UTF-8 LC_MESSAGES="en_US.UTF-8" LC_PAPER=ru_RU.UTF-8 LC_NAME=ru_RU.UTF-8 LC_ADDRESS=ru_RU.UTF-8 LC_TELEPHONE=ru_RU.UTF-8 LC_MEASUREMENT=ru_RU.UTF-8 LC_IDENTIFICATION=ru_RU.UTF-8 LC_ALL=
#include <QCoreApplication> #include <QDomDocument> #include <QFile> #include <QTextStream> #include <QDebug> int main(int argc, char* argv[]) { QCoreApplication a(argc, argv); QLocale locale(QLocale::C); QLocale::setDefault(locale); QFile xmlFile("data.xml"); xmlFile.open(QIODevice::WriteOnly); QDomDocument xmlWriter; QDomElement rootNode = xmlWriter.createElement("Root"); xmlWriter.appendChild(rootNode); float floatVal = 0.1234f; double doubleVal = 0.1234; rootNode.setAttribute("DecimalPoint", locale.decimalPoint()); rootNode.setAttribute("floatVal", floatVal); rootNode.setAttribute("doubleVal", doubleVal); xmlFile.write(xmlWriter.toByteArray()); xmlFile.close(); QString textData = xmlWriter.toString(); QDomDocument xmlReader; xmlReader.setContent(textData); QDomElement root = xmlReader.documentElement(); double fVal = root.attribute("floatVal").toDouble(); double dVal = root.attribute("doubleVal").toDouble(); qDebug() << fVal << dVal; return a.exec(); }
-
@Tetsuzin72
If you do not get a better reply here, I would say it does look like a bug, and yes should be reported to Qt bug system. For this you need to tell us & them just which version of Qt you are using for this behaviour? -
The documentation is wrong here (and so the implementation):
The number is formatted according to the current locale.
For double this is true, for float internally QString::setNum() is called which explicitly states:
The formatting always uses QLocale::C, i.e., English/UnitedStates.
Now the question is - what does xml need. I would guess QLocale::C.
Please open a bug report, refer to this thread and assign it to me. :)
-
And QDomElement::setAttributeNS(..., float) doesn't exist at all... hmm
-
Helo! I'v opened a bug report:
QTBUG-80068 -
sometimes strange thing happen.
Someone decided to even translate
printf
output, which affects programs likegdb
itself printing and readin float numbers. you can workaround this withLC_NUMERIC
, but that influences your own program too... A mess!Regards