1, set the display precision of floating-point number
Set number of decimal places for floating-point output set 4-bit decimal output//mode 1 COUT.SETF (Ios_base::fixed,ios::floatfield); cout.precision (4);//mode 2//use Control To include Iomanip header files cout << std::fixed << std::setprecision (4);
2, C + + IO stream simple Read and write files
Double b = 123451.45;//Write text file string File_path ("D:\\test\\a.txt"); Ofstream fo;fo.open (file_path,ios_base::out | ios_ Base::trunc), if (!fo.is_open ()) { cout << "File Open failed!" << Endl; return;} Fo << std::fixed;fo << std::setprecision (4) << b;fo << endl;fo << std::setprecision (2) < < B;fo.close ();//Read file cout << "read file\n"; Ifstream fi;fi.open (File_path,ios_base::in); if (!fi.is_open ()) { cout << "file can not open!" << Endl; return;} String Str;while (!fi.eof ()) { std::getline (FI,STR); cout << str << Endl;} Fi.close ();
C + + read and write files