do_put
Defined in header <locale> | ? | ? |
---|---|---|
public: iter_type put(iter_type out, bool intl, std::ios_base& f, char_type fill, long double quant) const; | (1) | ? |
iter_type put(iter_type out, bool intl, std::ios_base& f, char_type fill, const string_type& quant) const; | (2) | ? |
protected: virtual iter_type do_put(iter_type out, bool intl, std::ios_base& str, char_type fill, long double units) const; | (3) | ? |
virtual iter_type do_put(iter_type out, bool intl, std::ios_base& str, char_type fill, const string_type& digits) const; | (4) | ? |
¸ñʽ»¯»õ±ÒÖµ²¢½«½á¹ûдÈëÊä³öÁ÷¡£
1-2%29¸ö¹«¹²³ÉÔ±º¯Êý£¬µ÷ÓóÉÔ±º¯Êýdo_put
×îÅÉÉúµÄÀà¡£
3%29ÊýÖµ²ÎÊýunits
ת»»Îª¿í×Ö·û´®£¬¾ÍÏñct.widen(buf1, buf1 +std::sprintf(buf1, "%.0Lf", units), buf2)
£¬ÔÚÄÄÀïct
ÊÇstd::ctype
СÃæstr.getloc()
ºÍbuf1
ºÍbuf2
×ã¹»´óµÄ×Ö·û»º³åÇø¡£Éú³ÉµÄ×Ö·û´®¡£buf2
±»´¦Àí¡¢¸ñʽ»¯ºÍÊä³öµ½out
¾ÍÏñÏÂÃæËùÃèÊöµÄÄÇÑù¡£
×Ö·û´®²ÎÊýµÄ4%29digits
£¬ÔòÖ»ÓÐͨ¹ý±È½ÏÒÔÏ·½·¨È·¶¨µÄ¿ÉÑ¡Ç°µ¼¼õºÅ%28 ASct.widen('-')
£¬ÔÚÄÄÀïct
ÊÇstd::ctype
СÃæstr.getloc()
%29ºÍÒÔÏÂÊý×Ö×Ö·û%28±»ct
%29×÷ΪҪ´¦Àí¡¢¸ñʽ»¯ºÍÊä³öµ½out
ÈçÏÂËùÊö¡£
¸ø¶¨Ç°Ãæ²½ÖèÖеÄ×Ö·ûÐòÁУ¬Èç¹ûµÚÒ»¸ö×Ö·ûµÈÓÚct.widen('-')
£¬µç»°mp.neg_format()
ÈôÒª»ñµÃ¸ñʽÉèÖã¬ÇëÖ´ÐÐÒÔϲÙ×÷pattern
£¬·ñÔòµ÷ÓÃmp.pos_format()
£¬ÔÚÄÄÀïmp
ÊÇstd::moneypunct<CharT, intl>
СÃæstr.getloc()
...
°´ÒªÇó²åÈëÊýǧ¸ö·Ö¸ô·ûºÍСÊýµã×Ö·û¡£mp.grouping()
£¬£¬£¬mp.frac_digits()
£¬£¬£¬mp.decimal_point()
£¬ºÍmp.thousands_sep()
£¬²¢½«½á¹û×Ö·û´®·ÅÖÃÔÚÊä³öÐòÁÐÖУ¬ÆäÖÐvalue
³öÏÖÔÚ¸ñʽÉèÖÃģʽÖС£
Èç¹ûstr.flags() & str.showbase
ÊÇ·ÇÁã%28std::showbase
²Ù×÷³ÌÐòʹÓÃ%29£¬È»ºóͨ¹ýµ÷ÓÃmp.curr_symbol()
²¢½«ÆäÖÃÓÚÊä³öÐòÁÐÖУ¬ÆäÖÐsymbol
³öÏÖÔÚ¸ñʽÉèÖÃģʽÖС£
Èç¹ûmp.positive_sign()
%28ÔÚʹÓÃ%29»òmp.negative_sign()
%28Èç¹ûʹÓøº¸ñʽģʽ£¬%29·µ»ØÒ»¸ö×Ö·û´®£¬ÆäÖаüº¬¶à¸ö×Ö·û£¬·µ»ØµÄµÚÒ»¸ö×Ö·û·ÅÔÚÊä³öÐòÁÐÖÐsign
³öÏÖÔÚ¸ñʽ»¯Ä£Ê½ÖУ¬ÆäÓà×Ö·û·ÅÔÚËùÓÐÆäËû×Ö·ûÖ®ºó£¬ÀýÈç¸ñʽ»¯Ä£Ê½¡£{sign, value, space, symbol
Óе¥Î»123
ºÍÏû¼«[Ò½]±êÖ¾"-"
¿ÉÄܵ¼ÖÂ"-1.23 €"
£¬¶ø²»ÊÇ[Ò½]±êÖ¾"()"
»á²úÉú"(1.23 €)"
...
Èç¹ûΪָ¶¨¸ñʽÉú³ÉµÄ×Ö·ûÊýСÓÚstr.width()
µÄ¸±±¾fill
²åÈëÒÔʹÊä³öÐòÁеÄ×ܳ¤¶È¾«È·µØ´ïµ½¡£str.width()
£¬ÈçÏ£º
- Èç¹û
str.flags() & str.adjustfield
µÈºÅstr.internal
£¬Ôò²åÈëÌî³ä×Ö·ûµÄλÖá£none
»òspace
³öÏÖÔÚ¸ñʽÉèÖÃģʽÖС£
- ·ñÔò£¬Èç¹û
str.flags() & str.adjustfield
µÈºÅstr.left
µÄ¸±±¾fill
ÔÚËùÓÐÆäËû×Ö·ûÖ®ºó×·¼Ó¡£
- ·ñÔò£¬Ìî³ä×Ö·û·ÅÔÚËùÓÐÆäËû×Ö·û֮ǰ¡£
×îºó£¬´òµç»°str.width(0)
È¡ÏûÈκÎstd::setw
...
·µ»ØÖµ
ÔÚ²úÉúµÄ×îºóÒ»¸ö×Ö·ûÖ®ºóÁ¢¼´Ö¸ÏòµÄµü´úÆ÷¡£
×¢¼Ç
ÕâЩ»õ±Òµ¥Î»±»ÈÏΪÊÇÕâÖÖ»õ±Ò×îСµÄ·ÇСÊýµ¥Î»£ºÔÚÃÀ¹úÊÇÃÀ·Ö£¬ÔÚÈÕ±¾ÊÇÈÕÔª¡£
Àý
¶þ´Î
#include <iostream>
#include <iomanip>
#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("ru_RU.utf8");
std::cout.imbue(loc);
long double units = -123.45;
std::cout << "In Russian locale, " << units << " prints as "
<< std::showbase;
// note, the following is equivalent to simply std::put_money(units)
std::use_facet<std::money_put<char>>(loc).put(
std::ostreambuf_iterator<char>(std::cout),
false, std::cout, std::cout.fill(), units);
std::cout << '\n';
std::cout.imbue(std::locale(std::cout.getloc(), new my_punct("ru_RU.utf8")));
std::cout << "With negative_sign set to \"()\", it prints as ";
std::use_facet<std::money_put<char>>(loc).put(
std::ostreambuf_iterator<char>(std::cout),
false, std::cout, std::cout.fill(), units);
std::cout << '\n';
}
¶þ´Î
²ú³ö£º
¶þ´Î
In Russian locale, -123,45 prints as -1.23 §â§å§Ò
With negative_sign set to "()", it prints as (1.23 §â§å§Ò)
¶þ´Î
Áí¼û
moneypunct | defines monetary formatting parameters used by std::money_get and std::money_put (class template) |
---|---|
money_get | parses and constructs a monetary value from an input character sequence (class template) |
put_money (C++11) | formats and outputs a monetary value (function template) |
? cppreference.com
ÔÚCreativeCommonsAttributionÏÂÊÚȨ-ShareAlikeδÒÆÖ²Ðí¿Év3.0¡£
±¾ÎĵµÏµÌÚѶÔÆ¿ª·¢ÕßÉçÇø³ÉÔ±¹²Í¬Î¬»¤£¬ÈçÓÐÎÊÌâÇëÁªÏµ cloudcommunity@tencent.com