Important: Please read the Qt Code of Conduct -

How to change decimal point in QLocale?

  • I use QLocale to format numbers that are then exported as a CSV file.
    Depending on the user's settings on the target machine (where the CSV file is opened), the numbers must use either the dot or the comma as a decimal separator.

    How do I specify the decimal separator to use in QLocale?

  • QLocale will follow the user's system locale so you should get the correct decimal point character by default provided you use the formatting functions built on QLocale. For example use QString::arg() with "%L1" or QLocale::toString() to format numbers, but do not use QString::number().

    See QLocale::system() and QLocale::setDefault()

  • The system where the CSV is exported might have a different locale than the system where the user wants to analyze it. Qt can't know the proper decimal separator for a different system, therefore I look for a way to set it.

  • I already told you how to change it for the entire application. You can also construct a QLocale for any supported language/country and use that in your number conversions when you parse the CSV file.

    const double value = 1234.5678;
    QString s;

    QLocale locale;
    s = locale.toString(value, 'f', 3);
    qDebug() << "System default:" << value << s << locale.toDouble(s);

    locale = QLocale("fr_FR");
    s = locale.toString(value, 'f', 3);
    qDebug() << "French locale:" << value << s << locale.toDouble(s);

    locale = QLocale(QLocale::German);
    s = locale.toString(value, 'f', 3);
    qDebug() << "German locale:" << value << s << locale.toDouble(s);

    QLocale::setDefault(QLocale(QLocale::Arabic, QLocale::Egypt));
    locale = QLocale();
    s = locale.toString(value, 'f', 3);
    qDebug() << "Changed default locale:" << value << s << locale.toDouble(s);
    System default: 1234.57 "1,234.568" 1234.57
    French locale: 1234.57 "1 234,568" 1234.57
    German locale: 1234.57 "1.234,568" 1234.57
    Changed default locale: 1234.57 "١٬٢٣٤٫٥٦٨" 1234.57

  • I don't need to change it for the entire application. I just need a QLocale object where the decimalPoint() method reliably returns either a "." or a "," (depending on what the user wants).

    Since I don't know which language locales on that system are configured to provide that, I just need setDecimalPoint(). Since that does not exist, how do I do it instead?

  • I have given you example locales with various permutations of decimal points and thousand separators. I have shown you how to use one without changing the whole application's default locale. I don't see why you are find it so difficult to chose one that suits.

  • Because I doubt that these locales are reliably behaving the same on all operating systems (Windows, Linux, Linux Embedded, Mac,...).

    Because I doubt that some Embedded Linux systems I deploy to even have all those locales.

    It's all very well that I can test the output of locale.decimalPoint() on my own system. It doesn't mean that it will reliably work on every other system.

    You did however give me a different idea:
    I could try looping through locales until I find one where the decimalPoint() matches what I need. Butt-ugly as code goes, but it might work.

Log in to reply