How to use setprecision in C++
我是
就像如果数字是
| 1 2 3 4 5 6 7 8 9 | #include <iomanip.h> #include <iomanip> int main() { double num1 = 3.12345678; cout << fixed << showpoint; cout << setprecision(2); cout << num1 << endl; } | 
但是它给了我一个错误,未定义的固定符号。
| 1 2 3 4 5 6 7 8 9 10 11 | #include <iomanip> #include <iostream> int main() { double num1 = 3.12345678; std::cout << std::fixed << std::showpoint; std::cout << std::setprecision(2); std::cout << num1 << std::endl; return 0; } | 
| 1 2 3 | #include <iostream> #include <iomanip> using namespace std; | 
为方便起见,您可以输入
| 1 2 3 4 5 6 7 8 | int main() { double num1 = 3.12345678; cout << fixed << showpoint; cout << setprecision(2); cout << num1 << endl; return 0; } | 
Replace These Headers
| 1 2 | #include <iomanip.h> #include <iomanip> | 
With These.
| 1 2 3 | #include <iostream> #include <iomanip> using namespace std; | 
而已...!!!
上面的答案是绝对正确的。 这是它的Turbo C ++版本。
| 1 2 3 4 5 6 7 8 9 10 | #include <iomanip.h> #include <iostream.h> void main() { double num1 = 3.12345678; cout << setiosflags(fixed) << setiosflags(showpoint); cout << setprecision(2); cout << num1 << endl; } | 
对于
| 1 2 3 4 5 6 7 8 9 10 | #include <iostream> #include <iomanip> int main(void) { float value; cin >> value; cout << setprecision(4) << value; return 0; } | 
| 1 2 3 4 5 6 7 8 9 10 | #include <bits/stdc++.h>                        // to include all libraries  using namespace std; int main() { double a,b; cin>>a>>b; double x=a/b; //say we want to divide a/b cout<<fixed<<setprecision(10)<<x; //for precision upto 10 digit return 0; } | 
输入:1987年31
输出:662.3333333333小数点后10位
下面的代码正确运行。
| 1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> #include <iomanip> using namespace std; int main() { double num1 = 3.12345678; cout << fixed << showpoint; cout << setprecision(2); cout << num1 << endl; } |