Decimal format conversion from one locale to another locale(en_US) Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
YuMERA
Knows some wx things
Knows some wx things
Posts: 30
Joined: Fri Jul 05, 2019 8:47 pm

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

Post by YuMERA » Thu Feb 13, 2020 12:55 am

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?

User avatar
doublemax
Moderator
Moderator
Posts: 14628
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax » Thu Feb 13, 2020 7:15 am

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.
Use the source, Luke!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Sun Jan 03, 2010 5:45 pm

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

Post by PB » Thu Feb 13, 2020 7:34 am

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.

User avatar
doublemax
Moderator
Moderator
Posts: 14628
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax » 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.
After wxLocale instance goes out of scope, what locale is set then - the previous one?
Yes, the old one is restored.
Use the source, Luke!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2240
Joined: Sun Jan 03, 2010 5:45 pm

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

Post by PB » Thu Feb 13, 2020 4:31 pm

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

User avatar
doublemax
Moderator
Moderator
Posts: 14628
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Post by doublemax » Thu Feb 13, 2020 5:06 pm

You're right. I only looked into the code for wxLocale, which uses SetThreadLocale and forgot about the wxSetlocale.
Use the source, Luke!

YuMERA
Knows some wx things
Knows some wx things
Posts: 30
Joined: Fri Jul 05, 2019 8:47 pm

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

Post by YuMERA » Fri Feb 14, 2020 1:41 am

Thanks for the helpful info.
This is what I needed : wxString::FromCDouble()/ToCDouble()

Post Reply