c++11 标准模板(STL)本地化库 - 平面类别(std::moneypunct) - 定义 std::money_get 与 std::money_put 所用的货币格式解析器的参数(三)

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

平面类别

定义 std::money_get 与 std::money_put 所用的货币格式解析器的参数

std::moneypunct

template< class CharT, bool International = false >
class moneypunct;

平面 std::moneypunct 封装货币值格式化偏好。流 I/O 操纵符 std::get_money 和 std::put_money 通过 std::money_get 和 std::money_put 用 std::moneypunct 分析货币值输入及格式化货币值输出。

继承图

标准库提供四个孤立(独立于本地环境)的特化:

定义于头文件 <locale>

std::moneypunct<char>提供 "C" 本地环境偏好的等价版本
std::moneypunct<wchar_t>提供 "C" 本地环境偏好的宽字符等价版本
std::moneypunct<char, true>提供 "C" 本地环境偏好的等价版本,带国际通货符号
std::moneypunct<wchar_t, true>提供 "C" 本地环境偏好的宽字符等价版本,带国际通货符号

另外, C++ 程序中构造的每个 locale 对象都实装这些特化的其自身(本地环境限定)版本。

成员类型

成员类型定义
char_typeCharT
string_typestd::basic_string<CharT>

提供用作千分隔符的字符

std::moneypunct<CharT,International>::thousands_sep, 
std::moneypunct<CharT,International>::do_thousands_sep

public:
char_type thousands_sep() const;

(1)

protected:
virtual char_type do_thousands_sep() const;

(2)

1) 公开成员函数,调用最终导出类的成员函数 do_thousands_sep

2) 返回在分析或格式化货币值整数部分时用作分隔符的字符。

返回值

用作千分隔符的 char_type 类型对象。在常用美国本地环境中,此为 ',' 或 L',' 。

调用示例 windows

#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
#include <exception>
#include <Windows.h>

std::vector<std::wstring> locals;

BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
    locals.push_back(pStr);
    return TRUE;
}

std::string stows(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
    setlocale(LC_ALL, "chs");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = 2 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, _Source, _Dsize);
    std::string result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

struct space_out : std::moneypunct<char>
{
    pattern do_pos_format()      const
    {
        return { {value, symbol, none, none} };
    }
    int do_frac_digits()         const
    {
        return 0;
    }
    char_type do_thousands_sep() const
    {
        return ',';
    }
    string_type do_grouping()    const
    {
        return "\003";
    }
};

int main()
{
    EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);

    for (std::vector<std::wstring>::const_iterator str = locals.begin();
            str != locals.end(); ++str)
    {
        if (stows(*str) == "x-IV_mathan")
        {
            continue;
        }

        std::locale locale(stows(*str));
        std::cout.imbue(locale);

        std::cout << locale.name() << " ";

        std::cout.imbue(locale);
        std::cout << "american locale: " << std::showbase
                  << std::put_money(1234.5) << std::endl;

        std::cout.imbue(std::locale(std::cout.getloc(), new space_out));
        std::cout << "locale with modified moneypunct: "
                  << std::put_money(12345678.0) << '\n';

    }

    return 0;
}

输出

de-DE_phoneb american locale: 12,35 €
locale with modified moneypunct: 12,345,678
es-ES_tradnl american locale: 12,35 €
locale with modified moneypunct: 12,345,678
hu-HU_technl american locale: 12,35 Ft
locale with modified moneypunct: 12,345,678
ja-JP_radstr american locale: \1,235
locale with modified moneypunct: 12,345,678
ka-GE_modern american locale: 12,35 ?
locale with modified moneypunct: 12,345,678
zh-CN_phoneb american locale: ¥12.35
locale with modified moneypunct: 12,345,678
zh-CN_stroke american locale: ¥12.35
locale with modified moneypunct: 12,345,678
zh-HK_radstr american locale: HK$12.35
locale with modified moneypunct: 12,345,678
zh-MO_radstr american locale: MOP12.35
locale with modified moneypunct: 12,345,678
zh-MO_stroke american locale: MOP12.35
locale with modified moneypunct: 12,345,678
zh-SG_phoneb american locale: $12.35
locale with modified moneypunct: 12,345,678
zh-SG_stroke american locale: $12.35
locale with modified moneypunct: 12,345,678
zh-TW_pronun american locale: NT$12.35
locale with modified moneypunct: 12,345,678
zh-TW_radstr american locale: NT$12.35
locale with modified moneypunct: 12,345,678

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值