摘自MSDN:SMANIP( int ) setprecision( int np );#include <iomanip.h>ParameternpAn integer that indicates the number of significant digits or significant decimal digits to be used for floating-point display.ResThis parameterized manipulator sets the stream’s internal floating-point precision variable to np. The default precision is six digits. If the display format is scientific or fixed, then the precision indicates the number of digits after the decimal point. If the format is automatic (neither floating point nor fixed), then the precision indicates the total number of significant digits. This setting remains in effect until the next change.

解决方案 »

  1.   

    这个函数是对IO流的输出精度做一个限制,举个例子(出自MSDN)include <iostream.h>
    #include <iomanip.h>void main()
    {
       double values[] = { 1.23, 35.36, 653.7, 4358.24 };
       char *names[] = { "Zoot", "Jimmy", "Al", "Stan" };
       for( int i = 0; i < 4; i++ )
          cout << setw( 6 )  << names[i]
               << setw( 10 ) << values[i] << endl;
    }
    输出为:
     Zoot       1.23
    Jimmy      35.36
       Al      653.7
     Stan    4358.24对输出部分做修改:
    for ( int i = 0; i < 4; i++ )
       cout << setiosflags( ios::left )
            << setw( 6 )  
            << names[i]
            << resetiosflags( ios::left )
            << setw( 10 ) 
            << setprecision( 1 )
            << values[i] 
            << endl;
    输出为: 
    Zoot          1
    Jimmy     4e+001
    Al        7e+002
    Stan      4e+003
    此时,输出部分的整数部分精度已经设为了1在for之前加上
    cout << setiosflags( ios::fixed );
    则输出变为:
    Zoot         1.2
    Jimmy       35.4
    Al         653.7
    Stan      4358.2如果将ios::fixed 变为 ios::scientific 输出为:
    Zoot    1.2e+000
    Jimmy   3.5e+001
    Al      6.5e+002
    Stan    4.4e+003