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. [solved] QLocale - with atof - why doesn't it convert "3.3"?
Forum Updated to NodeBB v4.3 + New Features

[solved] QLocale - with atof - why doesn't it convert "3.3"?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.2k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    agold2012
    wrote on last edited by
    #1

    Hi, I've been having a problem for quite some while now with the Qt locale.
    I'm using Qt 4.6.2 on Ubuntu 10.04 64. In my code I'm using a library that uses the function
    atof (from stdlib.h), normally this would return for example:
    atof("4.4") = 4.4; but in my case it works only with atof("4,4")=4.4
    Since my System is set to ES locale, I understand that it uses commas, but when I try to set the locale to US-EN, it still doesn't work:

    @
    #include <QtCore/QCoreApplication>
    #include <QLocale>

    #include <iostream>
    #include <cmath>
    #include <cstdlib>

    using namespace std;

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));

    cout << "arg[1]   : " << argv[1] << endl;
    cout << "atof(arg): " << atof(argv[1])<<endl;
    

    return 0;
    }
    @

    This gives:
    @$ ./mtest 3.3
    arg[1] : 3.3
    atof(arg): 3

    $ ./mtest 3,3
    arg[1] : 3,3
    atof(arg): 3.3
    @

    Anyone knows how I can change the locale such that I don't have to change the library's code?

    Thanks in advance!

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      atof doesn't care about [[Doc:QLocale]] settings and QLocale doesn't change the posix locale of the process. You need to set the environment variable LANG (or LC_NUMERIC).

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        agold2012
        wrote on last edited by
        #3

        Thanks! It worked using setlocale(..)!

        @
        #include <QtCore/QCoreApplication>
        //#include <QLocale>
        #include <locale.h>

        #include <iostream>
        #include <cmath>
        #include <cstdlib>

        using namespace std;

        int main(int argc, char *argv[])
        {
        QCoreApplication a(argc, argv);

        // QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
        setlocale(LC_NUMERIC,"en_US");

        cout << "arg[1] : " << argv[1] << endl;
        cout << "atof(arg): " << atof(argv[1])<<endl;
        return 0;
        }
        @

        1 Reply Last reply
        0

        • Login

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