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