You are here:

C++/local sensitive formatting


Hi Ralph,

I'm having trouble with advice you sent me concerning local sensitive formatting.

There was no trouble with the code you sent:

#include <iostream>
#include <ios>
#include <locale>

int main()
double dblcMeterConvert(0.0);
dblcMeterConvert = 500000 / 25.4;

std::locale system_locale("");
std::cout << std::setprecision(10);

std::cout << "dblcMeterConvert = "
<< std::showpoint << dblcMeterConvert
<< std::endl;
return 0;

As I said all works well within the console application.

There is one problem with the above code within the variables/context window: the variable still only shows in the decimal-period format and leaving the cursor over the variable within the code itself only shows the variable in the decimal-period format. I only mention this because I can't tell where my program is having problems changing into the decimal-comma format.

Here is a small piece of my code. As a brief explanation, an individual value of an array is being placed as a string into an edit box to be viewed/changed by the user.

dblOrigHgtNmbr_TB[intI] = (double)lngArray[lngBaseAlt+intJ] / dblcMtrCnv;

std::locale system_locale("");
std::cout << std::setprecision(10);

std::ostringstream oss;
oss << dblOrigHgtNmbr_TB[intI];
std::string height = oss.str();

CString strText=_T(height.c_str());
CEdit* pStartAlt = (CEdit*)GetDlgItem(IDC_EDIT_STRT_ALT_00);

The problem is that the edit box shows the variable (the individual value of the array) in decimal-period format ignoring your code (std::locale system_locale("");etc.). I suppose that I should be changing the variable into a float rather than a string... This is a 'hangover' from my abilities with Visual Basic which demanded a string be placed into an edit box and input into initial edit boxes within my program have to be strings... A bit of a shortcut - 'one size fits all'.

Thanks in advance for your help,

OK in your code std::cout is a stream and oss is a stream. You have imbued the std::cout stream with system_locale but what about the oss stream? I see no matching call to imbue for oss.

The stream imbue operation works on a stream object by object basis. Imbuing one stream with a certain locale does not imbue any others with the same locale. Read my original answer if you wish all streams created after a certain point to pick up a specific locale and the use of std::locale::global. Note the point about not replacing locales for existing stream objects (e.g. std::cout, std::cin, std::cerr), in such cases use a form such as

   std::cout.imbue( std::locale() );
to imbue existing streams with the current global locale.

Hope this helps.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.