C++中的流操纵器的science()方法用于为指定的str流设置floatfield格式标志。该标志将浮点数设置为科学。这意味着,浮点值将以科学计数法表示。
句法:
ios_base& scientific (ios_base& str)
参数:此方法接受str作为参数,这是影响格式标志的流。
返回值:该方法返回设置了内部格式标志的流str。
范例1:
// C++ code to demonstrate
// the working of scientific() function
#include
using namespace std;
int main()
{
// Initializing the float values
double x = 1.23;
cout.precision(5);
cout << "without scientific flag: "
<< x << endl;
// Using scientific()
cout << "with scientific flag: "
<< scientific << x << endl;
return 0;
}
输出:
without scientific flag: 1.23
with scientific flag: 1.23000e+00
范例2:
// C++ code to demonstrate
// the working of scientific() function
#include
using namespace std;
int main()
{
// Initializing the float values
double x = 1.0;
cout.precision(5);
cout << "without scientific flag: "
<< x << endl;
// Using scientific()
cout << "with scientific flag: "
<< scientific << x << endl;
return 0;
}
输出:
without scientific flag: 1
with scientific flag: 1.00000e+00
范例3:
// C++ code to demonstrate
// the working of scientific() function
#include
using namespace std;
int main()
{
// Initializing the float values
double x = 1.23e9;
cout.precision(5);
cout << "without scientific flag: "
<< x << endl;
// Using scientific()
cout << "with scientific flag: "
<< scientific << x << endl;
return 0;
}
输出:
without scientific flag: 1.23e+09
with scientific flag: 1.23000e+09
参考: http://www.cplusplus.com/reference/ios/scientific/
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。