1

I have a very basic question: how can I enforce the use of points in floating-point numbers instead of a comma (I have a french version of my OS) in Qt?

Other question :is it possible to display numbers with space for thousands separators?

Bart
  • 19,692
  • 7
  • 68
  • 77
Vincent
  • 57,703
  • 61
  • 205
  • 388
  • 1
    You might want to have a look at [QLocale](http://qt-project.org/doc/qt-4.8/qlocale.html) and its functionalities. – Bart Jul 22 '12 at 16:56
  • Try `setlocale` together with the `%'d` format specifier for `printf` to get the thousands separator (though I appreciate that that's not a Qt answer). – Kerrek SB Jul 22 '12 at 16:58
  • 2
    Please make it a separate question for your second one. Thanks. – Stephen Chu Jul 22 '12 at 18:53

2 Answers2

4

Try this:

QLocale loc = QLocale::system(); // current locale
loc.setNumberOptions(QLocale::c().numberOptions()); // borrow number options from the "C" locale
QLocale::setDefault(loc); // set as default

If you want all of the options as in the "C" locale, you can simply do

QLocale::setDefault(QLocale::c());

Regarding your second question: Qt does not support custom locales, but you can try setting the number options to, say, Hungary's locale (it should produce 1234 and 12 345.67 - I haven't tried it myself)

QLocale loc = QLocale::system(); // current locale
QLocale hungary(QLocale::Hungarian);
loc.setNumberOptions(hungary.numberOptions()); // borrow number options from the Hungarian locale
QLocale::setDefault(loc); // set as default
Oleg2718281828
  • 1,039
  • 7
  • 17
0

For Linux:

Since I live in Germany but want my system to use english messages, I have a mixed locale on my PC:

LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=de_DE.UTF-8
LC_TIME=de_DE.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=de_DE.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=de_DE.UTF-8
LC_NAME=de_DE.UTF-8
LC_ADDRESS=de_DE.UTF-8
LC_TELEPHONE=de_DE.UTF-8
LC_MEASUREMENT=de_DE.UTF-8
LC_IDENTIFICATION=de_DE.UTF-8
LC_ALL=

This caused some trouble with the representation of numbers.

Because I was new to QLocale and was on a time budget, I used a simple hack to "fix" the problem temporarily and it worked quite well for me:

int main(int argc, char *argv[]) {
  // make numbers predictable
  // a preliminary hack. Will be fixed with native language support
  setenv("LC_NUMERIC", "C",1); 
  QApplication a(argc, argv);
...

This had the advantage, that numbers got unified in both, on-screen presentation and reading and writing from and to persistent storage. The latter one was the biggest issue as e.g. floats got written like '123,45' instead of '123.45'.

Not a native solution, but a viable trick to get ahead for the time being.

Just to be complete: The application was just for myself. So this trick does of course not claim any professionality.

emax
  • 307
  • 3
  • 7