Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::money_get<CharT,InputIt>::get, do_get

      From cppreference.com
      <cpp‎ |locale‎ |money get
       
       
       
      Localization library
       
      std::money_get
      Member functions
      money_get::getmoney_get::do_get
       
      Defined in header<locale>
      public:

      iter_type get( iter_type beg, iter_type end,bool intl,std::ios_base& str,

                     std::ios_base::iostate& err,longdouble& units)const;
      (1)
      iter_type get( iter_type beg, iter_type end,bool intl,std::ios_base& str,
                     std::ios_base::iostate& err, string_type& digits)const;
      (2)
      protected:

      virtual iter_type do_get( iter_type beg, iter_type end,bool intl,std::ios_base& str,

                               std::ios_base::iostate& err,longdouble& units)const;
      (3)
      virtual iter_type do_get( iter_type beg, iter_type end,bool intl,std::ios_base& str,
                               std::ios_base::iostate& err, string_type& digits)const;
      (4)

      Parses monetary value from an input iterator and writes the result to alongdouble or string.

      1,2) Public member functions, call the member functiondo_get of the most derived class.
      3,4) Reads characters from the input iteratorbeg, expecting to find a monetary value formatted according to the rules specified by thestd::ctype facet imbued instr.getloc() (ct for the rest of this page), thestd::moneypunct<CharT, intl> facet imbued instr.getloc() (mp for the rest of this page), and the stream formatting flags obtained fromstr.flags().

      If the input iteratorbeg becomes equal toend before the parsing was completed, sets bothfailbit andeofbit inerr. If parsing fails for another reason, sets thefailbit inerr. Either way, does not modify the output parameter (units ordigits) on error.

      If the parsing succeeds, does not changeerr, and stores the result inunits ordigits.

      The formattingpattern used by this function is alwaysmp.neg_format().

      Ifmp.grouping() does not permit thousands separators, the first separator encountered is treated as a parsing error, otherwise they are treated as optional.

      Ifmoney_base::space ormoney_base::none is the last element in thepattern, the parser does not attempt to consume any whitespace after the other components of the monetary value were parsed. Otherwise, one or more whitespace characters are consumed wheremoney_base::space appears.

      Ifshowbase flag is set instr.flags(), the currency symbol or currency string is required, if it is not set, the currency symbol is optional.

      If the first character of the string returned bymp.positive_sign() ormp.negative_sign() is found in themoney_base::sign position of the formatting pattern, it is consumed, and the rest of the characters in that string are expected and consumed after all other components of the monetary value. If bothmp.positive_sign() andmp.negative_sign() are non-empty, the sign is required and must match the first character of one of these strings. If one of theses strings is empty, the sign is optional (and if it is absent, the sign of the result corresponds to the string that was empty). If both strings are empty, or have the same first character, the result is given the positive sign. If the output parameter is a string (digits) and the result is negative, the valuect.widen('-') is stored as the first character of the result.

      Digits from the input are extracted in order in which they appear and are placed indigits (after widening byct.widen() as necessary), or into a temporary bufferbuf1, from which the value ofunits is constructed as if by

      staticconstchar src[]="0123456789-";CharT atoms[sizeof(src)];ct.widen(src, src+ sizeof(src)-1, atoms);for(int i=0; i< n;++i)buf2[i]= src[find(atoms, atoms+sizeof(src), buf1[i])- atoms];buf2[n]=0;sscanf(buf2,"%Lf",&units);

      (wheren is the number of characters extracted from the input and stored inbuf1 andbuf2 is another sufficiently large character buffer).

      Contents

      [edit]Return value

      An iterator pointing immediately after the last character recognized as a valid part of the monetary string input.

      [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. Thus, the input sequence"$1,056.23" in a U.S. locale produces the number105623.0 inunits or a string"105623" indigits.

      Because currency symbol is optional ifshowbase is off but the entire multicharacternegative_sign() is required, given the formatting pattern{sign, value, space, symbol} withshowbase off and negative_sign of"-", the string"-1.23 €" parses as-123 and leaves "€" unconsumed on the input stream, but if negative_sign is"()", the string"(1.23 €)" is consumed completely.

      The I/O manipulatorstd::get_money offers a simpler interface to this function.

      [edit]Example

      Run this code
      #include <iostream>#include <locale>#include <sstream> void demo_money_get(std::locale loc,conststd::string& input){std::istringstream str(input);    str.imbue(loc);longdouble units; // The following can be written simpler with std::get_money(units)std::ios_base::iostate err=std::ios_base::goodbit;std::istreambuf_iterator<char> ret=std::use_facet<std::money_get<char>>(loc).get(std::istreambuf_iterator<char>(str),std::istreambuf_iterator<char>(),false, str, err, units);    str.setstate(err);std::istreambuf_iterator<char> last{};if(str){std::cout<<"Successfully parsed '"<< str.str()<<"' as "<< units/100<<" units\n";if(ret!= last){std::cout<<"Remaining content: '";std::copy(ret, last,std::ostreambuf_iterator<char>(std::cout));std::cout<<"'\n";}elsestd::cout<<"The input was fully consumed\n";}else{std::cout<<"Parse failed. Unparsed string: '";std::copy(ret, last,std::ostreambuf_iterator<char>(std::cout));std::cout<<"'\n";}} int main(){    demo_money_get(std::locale("en_US.utf8"),"-$5.12 abc");    demo_money_get(std::locale("ms_MY.utf8"),"(RM5.12) def");}

      Output:

      Successfully parsed '-$5.12 abc' as -5.12 unitsRemaining content: ' abc'Successfully parsed '(RM5.12) def' as -5.12 unitsRemaining content: ' def'

      [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)
      parses a monetary value
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/money_get/get&oldid=160159"

      [8]ページ先頭

      ©2009-2025 Movatter.jp