命名空间
变体
操作

std::moneypunct<CharT,International>::positive_sign,do_positive_sign,negative_sign,do_negative_sign

来自 cppreference.cn
< cpp‎ | locale‎ | moneypunct
 
 
 
 
 
定义于头文件 <locale>
public:
string_type positive_sign() const;
(1)
public:
string_type negative_sign() const;
(2)
protected:
virtual string_type do_positive_sign() const;
(3)
protected:
virtual string_type do_negative_sign() const;
(4)
1) 公有成员函数,调用最派生类的成员函数 do_positive_sign
2) 公有成员函数,调用最派生类的成员函数 do_negative_sign
3) 返回用于格式化正货币值的字符串。
3) 返回用于格式化负货币值的字符串。

仅有返回字符串的首字符是会出现在 pos_format()/neg_format() 位置的字符,由值 sign 指示。其余字符出现在货币字符串的后面

特别地,对于 "-" 的 negative_sign ,格式化可能显示为 "-1.23 €",而对于 "()" 的 negative_sign ,它将显示为 "(1.23 €)"

[edit] 返回值

类型为 string_type 的字符串,保有要用作正号或负号的字符。

[edit] 示例

#include <iomanip>
#include <iostream>
#include <locale>
 
struct my_punct : std::moneypunct_byname<char, false>
{
    my_punct(const char* name) : moneypunct_byname(name) {}
    string_type do_negative_sign() const { return "()"; }
};
 
int main()
{
    std::locale loc("de_DE.utf8");
    std::cout.imbue(loc);
    std::cout << loc.name() << " negative sign is '"
              << std::use_facet<std::moneypunct<char>>(loc).negative_sign()
              << "' for example: " << std::showbase << std::put_money(-1234) << '\n';
 
    std::locale loc2("ms_MY.utf8");
    std::cout.imbue(loc2);
    std::cout << loc2.name() << " negative sign is '"
              << std::use_facet<std::moneypunct<char>>(loc2).negative_sign()
              << "' for example: " << std::put_money(-1234) << '\n';
 
    std::cout.imbue(std::locale(std::cout.getloc(), new my_punct("de_DE.utf8")));
    std::cout << "de_DE.utf8 with negative_sign set to \"()\": "
              << std::put_money(-1234) << '\n';
}

输出

de_DE.utf8 negative sign is '-' for example: -12,34 €
ms_MY.utf8 negative sign is '()' for example: (RM12.34)
de_DE.utf8 with negative_sign set to "()": (12,34 €)

[edit] 参见

为货币值提供格式化模式
(virtual protected member function) [edit]