Ê×Ò³
ѧϰ
»î¶¯
רÇø
¹¤¾ß
TVP
·¢²¼
¾«Ñ¡ÄÚÈÝ/¼¼ÊõÉçȺ/ÓŻݲúÆ·,¾¡ÔÚС³ÌÐò
Á¢¼´Ç°Íù

std::money_put::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...

·µ»ØÖµ

ÔÚ²úÉúµÄ×îºóÒ»¸ö×Ö·ûÖ®ºóÁ¢¼´Ö¸ÏòµÄµü´úÆ÷¡£

×¢¼Ç

ÕâЩ»õ±Òµ¥Î»±»ÈÏΪÊÇÕâÖÖ»õ±Ò×îСµÄ·ÇСÊýµ¥Î»£ºÔÚÃÀ¹úÊÇÃÀ·Ö£¬ÔÚÈÕ±¾ÊÇÈÕÔª¡£

Àý

¶þ´Î

´úÂëÓïÑÔ£ºjavascript
¸´ÖÆ
#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';
}

¶þ´Î

²ú³ö£º

¶þ´Î

´úÂëÓïÑÔ£ºjavascript
¸´ÖÆ
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)

´úÂëÓïÑÔ£ºtxt
¸´ÖÆ
 ? cppreference.com

ÔÚCreativeCommonsAttributionÏÂÊÚȨ-ShareAlikeδÒÆÖ²Ðí¿Év3.0¡£

ɨÂë¹Ø×¢ÌÚѶÔÆ¿ª·¢Õß

ÁìÈ¡ÌÚѶÔÆ´ú½ðȯ

http://www.vxiaotou.com