Format thousands separator - c++

I am programming in c++ using mfc in visual studio 2010.
I live in Europe, so I have the regional settings of the PC not USA but Europe.
I use the .Format function of CString to print the result of a calculation and I want to add a decimal point as separator between hundred and thousands.
For example, I would like to have displayed 23.400 instead of 23400
Is it possible using a particular formatting of % or i have to change the setting of the pc?
Thanks for the help

As far as I know CString's .Format doesn't support this.
I'd use a stringstream to handle the formatting:
std::ostringstream temp;
temp.imbue(std::locale(""));
temp << 23400;
CString result = temp.str().c_str();
Specifying an empty string as the name of the locale as I've done here means it should pick up the locale setting from the OS. You can give the name of a specific locale instead (e.g., if you want a specific locale, regardless of how the OS is configured):
temp.imbue(std::locale("de")); // German locale

As far as I know this is what your are looking for. Simple search on google gave the answer. This type of print (%.2f) by the way is pretty standard among pretty much every modern language.
"Floating point: %.2f\n"
https://msdn.microsoft.com/en-us/library/aa314327(v=vs.60).aspx

Related

How to ensure rendering of digits as arabic numerals on Windows 10?

My application allows users to customize UI by selecting a user preferred language. It usually works great, except that on Windows 10, say, if a user locale is picked as, say, Cambodian in Windows Control Panel:
But then if the user in my app's UI picks US English, I can't seem to find a way to render it with "US English numbers." On Windows 8.1 it used to end up looking as such, no matter what locale is picked:
As my assumption was that one doesn't need to translate numbers. But on Windows 10, that same control ends up looking as such:
Note that its text is set up using just this call:
::SetWindowText(m_hWnd, L"1000");
So I am curious, is there any way to keep numbers rendered as the arabic numerals:
This issue goes much deeper than basic controls, it happens inside GDI and also affects DrawText and TextOut. The only documented way around it is to call ExtTextOut with the ETO_NUMERICSLATIN flag (or use Uniscribe to render text).
This behavior is completely by design
these flags only modify U+0030 -- U+0039, as needed
Becsause the truth is that GDI doesn't give a crap about formatting or really anything related to locales, with one signle exception: Digit Substitution
Any time you go to render text it will grab those digit substitution settings in the user locale (including the user override information) and use the info to decide how to display numbers.
Another thing that seems to work is to force a custom font with the GREEK_CHARSET charset. That charset triggers a font association magic feature. (EE_CHARSET also seems to work for English text). You would probably have to try to pick the best charset for each of your languages if you are going to do this but you cannot use ANSI_CHARSET nor DEFAULT_CHARSET.
If don't know why this only happens in Windows 10 but it really seems like a bug in certain places. In Explorer for example it will display "7-Zip" as "៧-Zip" etc.

Visual C++/MFC: getting Japanese characters to work without UNICODE

I have software originally developed 20 years ago in Visual C++ using MFC without UNICODE. Currently strings are held either in char[] or CString, and it works on English and Japanese Windows PCs until Japanese characters are used, as these tend to get converted to strange characters or empty boxes.
Setting UNICODE is presumably the way forward but will require a massive code change, whereas quite a lot seems to work simply by setting System Locale to Japan (in “Window’s Language for non-Unicode programs” setting). I have no idea how Windows does this, but some Japanese character things now work on my English Windows PC, e.g. I can open and save Japanese filenames with no code changes. And in Japan they set System Locale to English and again much works, but not everything.
I get the impression the problems are due to using a font that doesn’t include Japanese characters. Currently I am using Arial / MS Sans Serif and charset set to ANSI_CHARSET or DEFAULT_CHARSET. Is there a different font I should be using, or can I extend these fonts to include Japanese characters? Or am I barking up the wrong tree in which case what do I do next? Am very new to all this unfortunately …
That's a common question (OK I guess not so common any more in 2015, as MBCS programs luckily are a dying breed - I still maintain several though...)
Either way, I'm afraid that, depending on your definition of 'working', to get this working you'll have to bite the bullet and convert to a Unicode build. If you can't make a business case for that, then you'll have to set the right locale (well, worse, have the user set the 'right' one) and test what works and what doesn't, and ask more specific questions on what doesn't.
If your goal is to make one application that correctly displays strings in various encodings in the 'right' way regardless of the locale settings on the computer, and compatible with every input data set / database content without the user having to be aware of encoding issues, then you're out of luck with an MBCS build.
The font missing characters is most likely not the problem. Before you go any further and/or ask further questions, you should read http://www.joelonsoftware.com/articles/Unicode.html, read it again, sleep on it, read it again, explain to somebody else what the relationship is between 'encoding', 'locale', 'character set', 'font' and 'Unicode code point', because only after you can do that, you can decide on how to progress with your application. Sorry, it's not what you want to hear, but it's the reality if you've been tasked with handling internationalization.

Qt locale formatting of large currency (or number) on linux

I am trying to format a number as a currency and having problems. I've tried with both the default locale and a specific one (like below). The number is formatted in scientific notation nonetheless.
QLocale::setDefault( QLocale(QLocale::English, QLocale::UnitedStates) );
reportCost->setText( QString("$%L1").arg( cost ) );
The trouble is that as soon as I hit millions I get numbers like $1.6473e6 rather than $1,647,312. How can I fix this?
Also,is there really no way to format a currency at all in Qt, like positioning the symbol and proper negative representation?
I cannot test on Windows right now so I don't know if this is Linux specific.
Unfortunately, the current implementation of QLocale falls short in a lot of these areas. See "QLocale: It’s about time (and dates, and languages, and …)" for an explanation of where this is headed in the future.
For now, a quick-and-dirty solution would be:
QString("$%L1").arg(amt, 0, 'f', 2);
If you need real localization, the ICU libraries might be helpful.
I know that this is an old question, but it's worth pointing out to anyone who stumbles across this that in Qt 4.8 and above, the first thing to try should be QLocale::toCurrencyString(...)

Microsoft sublanguage string to locale identifier

I can't seem to find a way to convert, or find, a local identifier from a sublanguage string. This site shows the mappings:
http://msdn.microsoft.com/en-us/library/dd318693(v=VS.85).aspx
I want the user to enter a sublanguage string, such as "France (FR)" and to get the local identifier from this, which in this case would be 0x0484. Or the other way around, if a user enters 0x0480 then to return French (FR).
Has anyone encountered this problem before and can point me in the right direction?
Otherwise I'm going to be writing a few mapping statements to hard code it and maintain future releases if anything changes.
BTW, I'm coding in C++ for Windows platform.
Cheers
A good starting point would be the LCIDToLocaleName function and it's opposite - LocaleNameToLCID. Note that these allow converting between LCID and RFC4646 locale name; to get the humanreadable country and language names, use the GetLocaleInfoEx with the LOCALE_SENGLISH* flags. If you need localized names instead of English, use LOCALE_SLOCALIZED* constants instead.

How can I get full string value of variable in VC6 watch window?

I'm wanting to get the full value of a char[] variable in the VC6 watch window, but it only shows a truncated version. I can copy the value from a debug memory window, but that contains mixed lines of hex and string values. Surely there is a better way??
For large strings, you're pretty much stuck with the memory window - the tooltip would truncate eventually.
Fortunately, the memory window is easy to get data from - I tend to show it in 8-byte chunks so its easy to manage, find your string data and cut&paste the lot into a blank window, then use alt+drag to select columns and delete the hex values. Then start at the bottom of the string and continually page up/delete (the newline) to build your string (I use a macro for that bit).
I don't think there's any better way once you get long strings.
Push come to shove you can put in the watch
given
char bigArray[1000];
watch:
&bigArray[0]
&bigArray[100]
&bigArray[200]
...
or change the index for where in the string you want to look...
Its clunky, but its worked for me in the past.
I do not have VC6 any more, so I cannot try it. I do not know if it works, but maybe you can enter
(char*)textArray;
in the watch window.
The bettter solution maybe: VS2008 automatically displays the text the way you want. And there is a Express Edition for VS2008 free of change, which can, as far as I know, be used to develop commerecial applications. You can even try to continue developing with VC6, and use VS2008 for debugging only. With VS2003 it was possible. About 5 year ago I had to maintain an app which was developed with VC6. I kept using VC6 for developing, but for debugging I used VS2003.
The only technique i have seen is to watch the string
then the string + 50, + 100 etc.
Eugene Ivakhiv wrote an addin for msvc 6 that lets you display the full string in an edit box.
There's a cute plugin for VC6 called XDebug. It adds a dialog for viewing different types of strings. It worked great for me.
Perhaps, get used to creating logfiles, and write output into the file directly, then bring up in your favorite text editor.