For Programmers: Free Programming Magazines  


Home > Archive > VC STL > March 2005 > Inconsistent Application of Locale Settings









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Inconsistent Application of Locale Settings
JH Trauntvein

2005-03-17, 4:01 pm

I ran into a problem last w in a co-worker's program where the
locale information was not applied in the manner that we expected. We
had the operating system (windows XP) regional settings set to
Brasilian Portuguese and had the program imbueing a std::ostringstream
object with a locale created using std::locale(""). When a floating
point value (123456.789) was formatted, we found that, rather than
being formatted as "123,456" rather than "123.456" as we expected based
upon the current regional settings. After the standard random
thrashing, we found that he had a call to setlocale(LC_ALL,"") in his
apps initialisation code and that we could fix the problem by
commenting out that invocation. The following code, compiled against
the multi-threaded libraries, will reproduce the undesired behaviour
provided that the OS regional settings is set to Brasilian Portuguese.
Commenting out setlocale() will make the code work properly.


#include <iostream>
#include <locale.h>

int main()
{
double const val = 123456.789;

setlocale(LC_ALL,"");
std::cout.imbue(std::locale("Portuguese_Brazil"));
std::cout << val << std::endl;
return 0;

} // main


Regards,

Jon Trauntvein

JH Trauntvein

2005-03-18, 4:03 pm


JH Trauntvein wrote:
> I ran into a problem last w in a co-worker's program where the
> locale information was not applied in the manner that we expected.

We
> had the operating system (windows XP) regional settings set to
> Brasilian Portuguese and had the program imbueing a

std::ostringstream
> object with a locale created using std::locale(""). When a floating
> point value (123456.789) was formatted, we found that, rather than
> being formatted as "123,456" rather than "123.456" as we expected

based
> upon the current regional settings. After the standard random
> thrashing, we found that he had a call to setlocale(LC_ALL,"") in his
> apps initialisation code and that we could fix the problem by
> commenting out that invocation. The following code, compiled

against
> the multi-threaded libraries, will reproduce the undesired behaviour
> provided that the OS regional settings is set to Brasilian

Portuguese.
> Commenting out setlocale() will make the code work properly.
>
>
> #include <iostream>
> #include <locale.h>
>
> int main()
> {
> double const val = 123456.789;
>
> setlocale(LC_ALL,"");
> std::cout.imbue(std::locale("Portuguese_Brazil"));
> std::cout << val << std::endl;
> return 0;
>
> } // main
>
>
> Regards,
>
> Jon Trauntvein



I just realised that I forgot to mention that I am using visual c++
..net version 7.1

Nikola Dudar [MSFT]

2005-03-19, 4:00 am

setlocale("") is not the problem. This is NOP, it does not do anything aside
of just returning a string with the name of the active locale. The problem
is that when you set brazilian-portuguez locale, cout uses "," as delimiter
when it should use "." This is fixed in VS2005. As a workaround try using
CRT functions as sprintf().

Thanks,
Nikola

--
Nikola Dudar
Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Suggestions? Bugs? Feedback? Talk directly to dev teams using
http://lab.msdn.microsoft.com/productfeedback/


"JH Trauntvein" <j.trauntvein@comcast.net> wrote in message
news:1111148396.303055.246900@l41g2000cwc.googlegroups.com...
>
> JH Trauntvein wrote:
> We
> std::ostringstream
> based
> against
> Portuguese.
>
>
> I just realised that I forgot to mention that I am using visual c++
> .net version 7.1
>



JH Trauntvein

2005-03-20, 4:03 pm


Nikola Dudar [MSFT] wrote:
> setlocale("") is not the problem. This is NOP, it does not do

anything aside
> of just returning a string with the name of the active locale. The

problem
> is that when you set brazilian-portuguez locale, cout uses "," as

delimiter
> when it should use "." This is fixed in VS2005. As a workaround try

using
> CRT functions as sprintf().


Actually, commenting out the invocation of setlocale() in the program
fixes the problem.

Regards,

Jon Trauntvein

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com