Qt Lang environment?

217 views Asked by At

I Have got an issue concerning Qt Locale environment when I execute the following code

QApplication(argc,argv) ;
float f = 42.5f
std::cout << std::to_string(f) ; // prints 42,5

Even if my computer got its locale to french I'd like my program to be compiled with us standard printing format (i.e. 42.5 ). Is there a way to do that with a compiler option ?

1

There are 1 answers

10
László Papp On

This works fine for me:

main.cpp

#include <QString>
#include <QDebug>
#include <QCoreApplication>
#include <QLocale>

int main(int argc, char **argv)
{
    QCoreApplication coreApplication(argc, argv);
    float f = 42.5f;
    qDebug() << QString::number(f, 'f', 1);
    QLocale locale;
    qDebug() << locale.toString(f, 'f', 1);
    return coreApplication.exec();
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

"42.5"
"42.5"

This may be interesting for you:

QString QString::number(double n, char format = 'g', int precision = 6) [static]

Returns a string equivalent of the number n, formatted according to the specified format and precision. See Argument Formats for details.

Unlike QLocale::toString(), this function does not honor the user's locale settings.