gpt4 book ai didi

c++ - C/C++ 从 wint_t 打印英镑符号

转载 作者:行者123 更新时间:2023-11-30 03:15:21 25 4
gpt4 key购买 nike

我在 Linux 系统上并将键盘设置设置为 UK,以便捕获并打印出英磅符号 (£)。

这是我的代码:

#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;
}

此外,我想将英镑符号 (£) 存储为字符串的一部分。我发现当存储宽字符时 std::string 不表示准确的大小......在这种情况下使用 wstring 更好吗?它提供更准确的尺寸吗?

最佳答案

您可以使用 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

关于c++ - C/C++ 从 wint_t 打印英镑符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57101699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com