Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::num_put

      From cppreference.com
      <cpp‎ |locale
       
       
       
      Localization library
       
       
      Defined in header<locale>
      template<

         class CharT,
         class OutputIt=std::ostreambuf_iterator<CharT>

      >class num_put;

      Classstd::num_put encapsulates the rules for formatting numeric values as strings. Specifically, the typesbool,long,unsignedlong,longlong,unsignedlonglong(since C++11),double,longdouble,void*, and of all types implicitly convertible to these (such asint orfloat) are supported. The standard formatting output operators (such ascout<< n;) use thestd::num_put facet of the I/O stream's locale to generate text representation of numbers.

      std-num put-inheritance.svg

      If astd::num_put specialization is not guaranteed to be provided by the standard library (see below), the behaviors of itsput() anddo_put() are not guaranteed as specified.

      Contents

      [edit]Specializations

      The standard library is guaranteed to provide the following specializations (they arerequired to be implemented by any locale object):

      Defined in header<locale>
      std::num_put<char> creates narrow string representations of numbers
      std::num_put<wchar_t> creates wide string representations of numbers

      In addition, the standard library is also guaranteed to provide every specialization that satisfies the following type requirements:

      [edit]Nested types

      Type Definition
      char_typeCharT
      iter_typeOutputIt

      [edit]Data members

      Member Description
      std::locale::idid[static] the identifier of thefacet

      [edit]Member functions

      constructs a newnum_put facet
      (public member function)
      invokesdo_put
      (public member function)

      [edit]Protected member functions

      destructs anum_put facet
      (protected member function)
      [virtual]
      formats a number and writes to output stream
      (virtual protected member function)

      [edit]Example

      Run this code
      #include <iostream>#include <iterator>#include <locale>#include <string> int main(){double n=1234567.89;std::cout.imbue(std::locale("de_DE.UTF-8"));std::cout<<"Direct conversion to string:\n"<<std::to_string(n)<<'\n'<<"Output using a german locale:\n"<<std::fixed<< n<<'\n'<<"Output using an american locale:\n"; // use the facet directlystd::cout.imbue(std::locale("en_US.UTF-8"));auto& f=std::use_facet<std::num_put<char>>(std::cout.getloc());    f.put(std::ostreambuf_iterator<char>(std::cout),std::cout,' ', n);std::cout<<'\n';}

      Possible output:

      Direct conversion to string:1234567.890000Output using a german locale:1.234.567,890000Output using an american locale:1,234,567.890000

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 427C++98num_put was guaranteed to accept anyCharT that
      meets the requirements for a character on which
      any of the iostream components can be instantiated
      only guarantees to acceptchar,
      wchar_t and other implementation-
      defined character types
      LWG 2392C++98only character typeCharT could be
      guaranteed to be accepted bynum_put
      can guarantee to accept implementation-
      defined character container types

      [edit]See also

      defines numeric punctuation rules
      (class template)[edit]
      parses numeric values from an input character sequence
      (class template)[edit]
      (C++11)
      converts an integral or floating-point value tostring
      (function)[edit]
      (C++11)
      converts an integral or floating-point value towstring
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/num_put&oldid=177715"

      [8]ページ先頭

      ©2009-2025 Movatter.jp