如何在 C++ 中打印最多 6 位十进制数字?
How can I print a a number upto 6 decimal digits in c++?
我正在尝试一个问题。我需要以 6 位十进制数字打印 ans。例如,如果 ans 是 64,我想打印 64.000000
我尝试了以下方式。
我做错了什么?
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin>>t;
float n;
while(t--)
{
cin>>n;
float s=(n-2)*180.000000;
float w=(s/n)*1.000000;
cout<<w*1.000000<<setprecision(6)<<endl;
}
return 0;
}
你可以利用std::fixed
:
#include <iostream>
#include <iomanip>
void MyPrint(int i)
{
double d = static_cast<double>(i);
std::cout << std::setprecision(6) << std::fixed << d << std::endl;
}
int main() {
MyPrint(64);
MyPrint(100);
return 0;
}
Running the above code online 结果如下:
64.000000
100.000000
我正在尝试一个问题。我需要以 6 位十进制数字打印 ans。例如,如果 ans 是 64,我想打印 64.000000 我尝试了以下方式。 我做错了什么?
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin>>t;
float n;
while(t--)
{
cin>>n;
float s=(n-2)*180.000000;
float w=(s/n)*1.000000;
cout<<w*1.000000<<setprecision(6)<<endl;
}
return 0;
}
你可以利用std::fixed
:
#include <iostream>
#include <iomanip>
void MyPrint(int i)
{
double d = static_cast<double>(i);
std::cout << std::setprecision(6) << std::fixed << d << std::endl;
}
int main() {
MyPrint(64);
MyPrint(100);
return 0;
}
Running the above code online 结果如下:
64.000000
100.000000