Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::money_put<CharT,OutputIt>::put, do_put

      From cppreference.com
      <cpp‎ |locale‎ |money put
       
       
       
      Localization library
       
      std::money_put
      Member functions
      money_put::putmoney_put::do_put
       
      Defined in header<locale>
      public:

      iter_type put( iter_type out,bool intl,std::ios_base& f,

                     char_type fill,longdouble 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,longdouble 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)

      Formats monetary value and writes the result to output stream.

      1,2) Public member functions, call the member functiondo_put of the most derived class.
      3) The numeric argumentsunits is converted to a wide character string as if byct.widen(buf1, buf1+std::sprintf(buf1,"%.0Lf", units), buf2), wherect is thestd::ctype facet imbued instr.getloc() andbuf1 andbuf2 are sufficiently large character buffers. The resulting character stringbuf2 is processed, formatted, and output toout as desribed below.
      4) From the string argumentdigits, only the optional leading minus sign (as determined by comparing toct.widen('-'), wherect is thestd::ctype facet imbued instr.getloc()) and the immediately following digit characters (as classified byct) are taken as the character sequence to be processed, formatted, and output toout as described below.

      Given the character sequence from the previous steps, if the first character equalsct.widen('-'), callsmp.neg_format() to obtain the formattingpattern, otherwise callsmp.pos_format(), wheremp is thestd::moneypunct<CharT, intl> facet imbued instr.getloc().

      Thousands separator and decimal point characters are inserted as required bymp.grouping(),mp.frac_digits(),mp.decimal_point(), andmp.thousands_sep(), and the resulting string is placed in the output sequence wherevalue appears in the formatting pattern.

      Ifstr.flags()& str.showbase is non-zero (thestd::showbase manipulator was used), then the currency symbol or string is generated by callingmp.curr_symbol() and placed in the output sequence wheresymbol appears in the formatting pattern.

      Ifmp.positive_sign() (in case positive format pattern is used) ormp.negative_sign() (in case negative format pattern is used) returns a string with more than one character, the first character returned is placed in the output sequence wheresign appears in the formatting pattern, and the rest of the characters are placed after all other characters, for example, formatting pattern{sign, value, space, symbol} with units123 and negative_sign of"-" may result in"-1.23 €", while negative_sign of"()" would generate"(1.23 €)".

      If the number of characters generated for the specified format is less than the value returned bystr.width(), then copies offill are inserted to bring the total length of the output sequence to exactlystr.width(), as follows:

      • Ifstr.flags()& str.adjustfield equalsstr.internal, the fill characters are inserted wherenone orspace appears in the formatting pattern.
      • Otherwise, ifstr.flags()& str.adjustfield equalsstr.left, the copies offill are appended after all other characters.
      • Otherwise, the fill characters are placed before all other characters.

      In the end, callsstr.width(0) to cancel the effects of anystd::setw.

      Contents

      [edit]Return value

      An iterator pointing immediately after the last character produced.

      [edit]Notes

      The currency units are assumed to be the smallest non-fractional units of the currency: cents in the U.S, yen in Japan.

      [edit]Example

      Run this code
      #include <iomanip>#include <iostream>#include <locale> struct my_punct:std::moneypunct_byname<char,false>{    my_punct(constchar* name): moneypunct_byname(name){}    string_type do_negative_sign()const{return"()";}}; int main(){std::locale loc("ru_RU.utf8");std::cout.imbue(loc);longdouble 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::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::cout},false,std::cout,std::cout.fill(), units);std::cout<<'\n';}

      Output:

      In Russian locale, -123,45 prints as -1.23 рубWith negative_sign set to "()", it prints as (1.23 руб)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 328C++98the format string used forstd::sprintf was"%.01f"corrected to"%.0Lf"

      [edit]See also

      defines monetary formatting parameters used bystd::money_get andstd::money_put
      (class template)[edit]
      parses and constructs a monetary value from an input character sequence
      (class template)[edit]
      (C++11)
      formats and outputs a monetary value
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/money_put/put&oldid=160149"

      [8]ページ先頭

      ©2009-2025 Movatter.jp