std:: fixed, std:: scientific, std:: hexfloat, std:: defaultfloat
| 
           定义于头文件
            
            
             <ios>
            
            | ||
| 
           
            
             
              std::
              
               ios_base
              
             
            
            
             &
            
            fixed
            
             (
            
            
             
              std::
              
               ios_base
              
             
            
            
             &
            
            str
            
             )
            
            
             ;
            
           
           | (1) | |
| 
           
            
             
              std::
              
               ios_base
              
             
            
            
             &
            
            scientific
            
             (
            
            
             
              std::
              
               ios_base
              
             
            
            
             &
            
            str
            
             )
            
            
             ;
            
           
           | (2) | |
| 
           
            
             
              std::
              
               ios_base
              
             
            
            
             &
            
            hexfloat
            
             (
            
            
             
              std::
              
               ios_base
              
             
            
            
             &
            
            str
            
             )
            
            
             ;
            
           
           | (3) | (C++11 起) | 
| 
           
            
             
              std::
              
               ios_base
              
             
            
            
             &
            
            defaultfloat
            
             (
            
            
             
              std::
              
               ios_base
              
             
            
            
             &
            
            str
            
             )
            
            
             ;
            
           
           | (4) | (C++11 起) | 
修改浮点数输出的默认格式化设置。
        floatfield
       
       设置为
       
        fixed
       
       ,如同调用
       
        
         str.
         
          setf
         
         
          (
         
         
          
           std::
           
            ios_base
           
           
            ::
           
           
            fixed
           
          
         
         ,
         
          
           std::
           
            ios_base
           
           
            ::
           
           
            floatfield
           
          
         
         
          )
         
        
       
       。
      
        floatfield
       
       设置为
       
        scientific
       
       ,如同调用
       
        
         str.
         
          setf
         
         
          (
         
         
          
           std::
           
            ios_base
           
           
            ::
           
           
            scientific
           
          
         
         ,
         
          
           std::
           
            ios_base
           
           
            ::
           
           
            floatfield
           
          
         
         
          )
         
        
       
       。
      
        floatfield
       
       设置为
       
        fixed
       
       和
       
        scientific
       
       ,如同调用
       
        
         str.
         
          setf
         
         
          (
         
         
          
           std::
           
            ios_base
           
           
            ::
           
           
            fixed
           
          
         
         
          |
         
         
          
           std::
           
            ios_base
           
           
            ::
           
           
            scientific
           
          
         
         ,
         
          
           std::
           
            ios_base
           
           
            ::
           
           
            floatfield
           
          
         
         
          )
         
        
       
       。这将启用十六进制浮点数格式化。
      
        floatfield
       
       设置为零,如同调用
       
        
         str.
         
          unsetf
         
         
          (
         
         
          
           std::
           
            ios_base
           
           
            ::
           
           
            floatfield
           
          
         
         
          )
         
        
       
       。这将启用默认浮点数格式化,该格式与固定格式和科学计数法格式不同。
      
       这是一个I/O操纵器,可以通过诸如
       
        
         out
         
          <<
         
         std
         
          ::
         
         
          fixed
         
        
       
       的表达式调用,其中
       
        out
       
       是
       
        
         std::basic_ostream
        
       
       类型的任意输出流(或通过诸如
       
        
         in
         
          >>
         
         std
         
          ::
         
         
          scientific
         
        
       
       的表达式调用,其中
       
        in
       
       是
       
        
         std::basic_istream
        
       
       类型的任意输入流)。
      
| 目录 | 
参数
| str | - | I/O 流的引用 | 
返回值
str (指向经过操作后的流的引用)。
注释
十六进制浮点数格式化会忽略流精度设置,这是由 std::num_put::do_put 的规范所要求的。
这些操纵符不会影响浮点数的解析。
示例
#include <iomanip> #include <iostream> #include <sstream> enum class cap { title, middle, end }; void print(const char* text, double num, cap c) { if (c == cap::title) std::cout << "┌──────────┬────────────┬──────────────────────────┐\n" "│ number │ iomanip │ representation │\n" "├──────────┼────────────┼──────────────────────────┤\n"; std::cout << std::left << "│ " << std::setw(8) << text << " │ fixed │ " << std::setw(24) << std::fixed << num << " │\n" << "│ " << std::setw(8) << text << " │ scientific │ " << std::setw(24) << std::scientific << num << " │\n" << "│ " << std::setw(8) << text << " │ hexfloat │ " << std::setw(24) << std::hexfloat << num << " │\n" << "│ " << std::setw(8) << text << " │ default │ " << std::setw(24) << std::defaultfloat << num << " │\n"; std::cout << (c != cap::end ? "├──────────┼────────────┼──────────────────────────┤\n" : "└──────────┴────────────┴──────────────────────────┘\n"); } int main() { print("0.0", 0.0, cap::title); print("0.01", 0.01, cap::middle); print("0.00001", 0.00001, cap::end); // 注意:选择 clang 以获得正确输出 double f; std::istringstream("0x1.8p+0") >> f; std::cout << "解析 0x1.8p+0 得到 " << f << '\n'; std::istringstream("0x1P-1022") >> f; std::cout << "解析 0x1P-1022 得到 " << f << '\n'; }
输出:
┌──────────┬────────────┬──────────────────────────┐ │ number │ iomanip │ representation │ ├──────────┼────────────┼──────────────────────────┤ │ 0.0 │ fixed │ 0.000000 │ │ 0.0 │ scientific │ 0.000000e+00 │ │ 0.0 │ hexfloat │ 0x0p+0 │ │ 0.0 │ default │ 0 │ ├──────────┼────────────┼──────────────────────────┤ │ 0.01 │ fixed │ 0.010000 │ │ 0.01 │ scientific │ 1.000000e-02 │ │ 0.01 │ hexfloat │ 0x1.47ae147ae147bp-7 │ │ 0.01 │ default │ 0.01 │ ├──────────┼────────────┼──────────────────────────┤ │ 0.00001 │ fixed │ 0.000010 │ │ 0.00001 │ scientific │ 1.000000e-05 │ │ 0.00001 │ hexfloat │ 0x1.4f8b588e368f1p-17 │ │ 0.00001 │ default │ 1e-05 │ └──────────┴────────────┴──────────────────────────┘ 解析 0x1.8p+0 得到 1.5 解析 0x1P-1022 得到 2.22507e-308
参阅
| 修改浮点数精度 (函数) |