Ik ben nieuw in C++
, ik wil alleen mijn puntnummer van maximaal 2 cijfers weergeven.
net zoals als het nummer 3.444
is, dan moet de uitvoer 3.44
zijn of als het nummer 99999.4234
is, dan moet de uitvoer 99999.42
, Hoe kan ik dat doen. de waarde is dynamisch. Hier is mijn code.
#include <iomanip.h>
#include <iomanip>
int main()
{
double num1 = 3.12345678;
cout << fixed << showpoint;
cout << setprecision(2);
cout << num1 << endl;
}
maar het geeft me een fout, een niet-gedefinieerd vast symbool.
Antwoord 1, autoriteit 100%
#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;
}
Antwoord 2, autoriteit 21%
#include <iostream>
#include <iomanip>
using namespace std;
U kunt de regel using namespace std;
. Anders moet u expliciet std::
toevoegen elke keer dat u cout
, fixed
, showpoint
, setprecision(2)
en endl
int main()
{
double num1 = 3.12345678;
cout << fixed << showpoint;
cout << setprecision(2);
cout << num1 << endl;
return 0;
}
Antwoord 3, autoriteit 8%
Het bovenstaande antwoord is absoluut correct. Hier is een Turbo C++ -versie ervan.
#include <iomanip.h>
#include <iostream.h>
void main()
{
double num1 = 3.12345678;
cout << setiosflags(fixed) << setiosflags(showpoint);
cout << setprecision(2);
cout << num1 << endl;
}
Voor fixed
en showpoint
, ik denk dat de setiosflags
-functie moet worden gebruikt.
Antwoord 4, Autoriteit 8%
Vervang deze headers
#include <iomanip.h>
#include <iomanip>
Hiermee.
#include <iostream>
#include <iomanip>
using namespace std;
Dat is het … !!!
Antwoord 5, Autoriteit 5%
std::cout.precision(2);
std::cout<<std::fixed;
Wanneer u Operator-overbelasting gebruikt, probeer dit dan.
Antwoord 6, Autoriteit 3%
Onderstaande code werkt correct.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double num1 = 3.12345678;
cout << fixed << showpoint;
cout << setprecision(2);
cout << num1 << endl;
}
Antwoord 7, Autoriteit 3%
#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;
}
invoer: 31 1987
uitvoer: 662.3333333333 10 cijfers achter de komma
Antwoord 8
#include <iostream>
#include <iomanip>
int main(void)
{
float value;
cin >> value;
cout << setprecision(4) << value;
return 0;
}