Page 1 of 1

Decimal format conversion from one locale to another locale(en_US)

Posted: Thu Feb 13, 2020 12:55 am
by YuMERA
I use wxLocale wxLANGUAGE_SERBIAN and have a decimal format like 1,23 I would like to convert it to decimal in format wxLANGUAGE_ENGLISH like 1.23 how?

Re: Decimal format conversion from one locale to another locale(en_US)

Posted: Thu Feb 13, 2020 7:15 am
by doublemax

Code: Select all

wxLocale locale(wxLANGUAGE_SERBIAN);

double f = (double)22/7;
wxLogMessage("%.2f", f);

::wxSetlocale(LC_NUMERIC, "C");
wxLogMessage("%.2f", f);
I don't know if this has any other, unwanted side effects.

Re: Decimal format conversion from one locale to another locale(en_US)

Posted: Thu Feb 13, 2020 7:34 am
by PB
doublemax wrote:
Thu Feb 13, 2020 7:15 am
wxLogMessage("%.2f", f);[/code]I don't know if this has any other, unwanted side effects.
Does changing the locale like this affect the current thread or all threads? After wxLocale instance goes out of scope, what locale is set then - the previous one?

Also, I think that if you use just "C" locale instead of a country-specific one, you can just use wxString::FromCDouble() and avoid changing the locale from Serbian altogether.

I use wxString::FromCDouble()/ToCDouble() all the time when I work with input/output requiring a decimal point when the application locale uses a decimal comma.

Re: Decimal format conversion from one locale to another locale(en_US)

Posted: Thu Feb 13, 2020 12:33 pm
by doublemax
Does changing the locale like this affect the current thread or all threads?
At least under Windows, it affects the current thread only.
After wxLocale instance goes out of scope, what locale is set then - the previous one?
Yes, the old one is restored.

Re: Decimal format conversion from one locale to another locale(en_US)

Posted: Thu Feb 13, 2020 4:31 pm
by PB
doublemax wrote:
Thu Feb 13, 2020 12:33 pm
Does changing the locale like this affect the current thread or all threads?
At least under Windows, it affects the current thread only.
Are you sure about that? I think wxWidgets use setlocale internally (in wxSetLocale) and MS documentation seems to suggest that setlocale by default affects all threads
https://docs.microsoft.com/en-us/cpp/pa ... ew=vs-2019

Re: Decimal format conversion from one locale to another locale(en_US)

Posted: Thu Feb 13, 2020 5:06 pm
by doublemax
You're right. I only looked into the code for wxLocale, which uses SetThreadLocale and forgot about the wxSetlocale.

Re: Decimal format conversion from one locale to another locale(en_US)

Posted: Fri Feb 14, 2020 1:41 am
by YuMERA
Thanks for the helpful info.
This is what I needed : wxString::FromCDouble()/ToCDouble()