Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDomDocument always uses system locale settings while writing double numbers in Linux.
Forum Updated to NodeBB v4.3 + New Features

QDomDocument always uses system locale settings while writing double numbers in Linux.

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 615 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Tetsuzin72T Offline
    Tetsuzin72T Offline
    Tetsuzin72
    wrote on last edited by
    #1

    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();
    }
    
    
    JonBJ 1 Reply Last reply
    0
    • Tetsuzin72T Tetsuzin72

      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();
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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?

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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. :)

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        5
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          And QDomElement::setAttributeNS(..., float) doesn't exist at all... hmm

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • Tetsuzin72T Offline
            Tetsuzin72T Offline
            Tetsuzin72
            wrote on last edited by Tetsuzin72
            #5

            Helo! I'v opened a bug report:
            QTBUG-80068

            1 Reply Last reply
            3
            • aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              sometimes strange thing happen.

              Someone decided to even translate printf output, which affects programs like gdb itself printing and readin float numbers. you can workaround this with LC_NUMERIC, but that influences your own program too... A mess!

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved