问题
I am on a Linux system and set the keyboard setting to UK in order to capture and print out a UK pound symbol (£).
Here is my code:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main ()
{
wint_t wc;
fputws (L"Enter text:\n", stdout);
setlocale(LC_ALL, "");
do
{
wc=getwchar();
wprintf(L"wc = %lc %d 0x%x\n", wc, wc, wc);
} while (wc != -1);
return 0;
}
Also, I wanted to store the UK pound symbol (£) as part of a string. I've found that std::string does NOT indicate an accurate size when wide characters are stored...is wstring much better to use in this case? Does it provide a more accurate size?
回答1:
You can use std::put_money
#include <iostream>
#include <sstream>
// Include the std::put_money and other utilities
#include <iomanip>
int main(int argc, char **argv)
{
// Value in cents!
const int basepay = 10000;
std::stringstream ss;
// Sets the local configuration
ss.imbue(std::locale("en_GB.utf8"));
ss << std::showbase << std::put_money(basepay);
std::cout << std::locale("en_GB.utf8").name() << ": " << ss.str() << '\n';
return 0;
}
Live
回答2:
Set the locale first before getting the input.
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main () {
setlocale(LC_ALL, "en_GB.UTF-8");
wchar_t wc;
fputws (L"Enter text:\n", stdout);
do {
wc = getwchar();
wprintf(L"wc = %lc %d 0x%x\n", wc, wc, wc);
} while (wc != -1);
return 0;
}
来源:https://stackoverflow.com/questions/57101699/c-c-printing-uk-pound-symbol-from-wint-t