Antworten:
Mit <iomanip>können Sie std::fixedund verwendenstd::setprecision
Hier ist ein Beispiel
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << d;
}
Und Sie erhalten Ausgabe
122.34
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(2)<<(x) vereinfacht die Verwendung auf:cout<<FIXED_FLOAT(d)
Sie waren fast da, müssen auch std :: fixed verwenden, siehe http://www.cplusplus.com/reference/iostream/manipulators/fixed/
#include <iostream>
#include <iomanip>
int main(int argc, char** argv)
{
float testme[] = { 0.12345, 1.2345, 12.345, 123.45, 1234.5, 12345 };
std::cout << std::setprecision(2) << std::fixed;
for(int i = 0; i < 6; ++i)
{
std::cout << testme[i] << std::endl;
}
return 0;
}
Ausgänge:
0.12
1.23
12.35
123.45
1234.50
12345.00
setprecision(n)gilt für die gesamte Zahl, nicht für den Bruchteil. Sie müssen das Festkommaformat verwenden, damit es auf den Bruchteil angewendet wird:setiosflags(ios::fixed)
Vereinfachen Sie die akzeptierte Antwort
Vereinfachtes Beispiel:
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed << std::setprecision(2) << d;
}
Und Sie erhalten Ausgabe
122.34
Referenz:
Ich hatte ein Problem mit ganzen Zahlen, während ich eine konsistente Formatierung wünschte.
Ein Umschreiben der Vollständigkeit halber:
#include <iostream>
#include <iomanip>
int main()
{
// floating point formatting example
double d = 122.345;
cout << std::fixed << std::setprecision(2) << d << endl;
// Output: 122.34
// integer formatting example
int i = 122;
cout << std::fixed << std::setprecision(2) << double(i) << endl;
// Output: 122.00
}
Ich hatte dieses ähnliche Problem in einem Codierungswettbewerb und so habe ich damit umgegangen. Festlegen einer Genauigkeit von 2 für alle Doppelwerte
Fügen Sie zuerst den Header hinzu, um setprecision zu verwenden
#include <iomanip>
Fügen Sie dann den folgenden Code in unser Hauptcode ein
double answer=5.9999;
double answer2=5.0000;
cout<<setprecision(2)<<fixed;
cout <<answer << endl;
cout <<answer2 << endl;
Ausgabe:
5.99
5.00
Sie müssen fest zum Schreiben von 5.00 verwenden, deshalb wird Ihre Ausgabe nicht für 5.00 kommen.
Ein kurzer Referenzvideolink, den ich hinzufüge, ist hilfreich
Verwenden Sie zuerst die folgenden 2 Stellen nach dem Dezimalpunkt:
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
Dann drucken Sie Ihre doppelten Werte.
Dies ist ein Beispiel:
#include <iostream>
using std::cout;
using std::ios;
using std::endl;
int main(int argc, char *argv[]) {
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
double d = 10.90;
cout << d << endl;
return 0;
}
mit Vorlagen
#include <iostream>
// d = decimal places
template<int d>
std::ostream& fixed(std::ostream& os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
return os;
}
int main(){
double d = 122.345;
std::cout << fixed<2> << d;
}
Ähnliches gilt auch für wissenschaftliche Zwecke, mit einer Breitenoption (nützlich für Spalten).
// d = decimal places
template<int d>
std::ostream& f(std::ostream &os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
return os;
}
// w = width, d = decimal places
template<int w, int d>
std::ostream& f(std::ostream &os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
os.width(w);
return os;
}
// d = decimal places
template<int d>
std::ostream& e(std::ostream &os){
os.setf(std::ios_base::scientific, std::ios_base::floatfield);
os.precision(d);
return os;
}
// w = width, d = decimal places
template<int w, int d>
std::ostream& e(std::ostream &os){
os.setf(std::ios_base::scientific, std::ios_base::floatfield);
os.precision(d);
os.width(w);
return os;
}
int main(){
double d = 122.345;
std::cout << f<10,2> << d << '\n'
<< e<10,2> << d << '\n';
}
Nur ein kleiner Punkt; Fügen Sie Folgendes in die Kopfzeile ein
Verwenden des Namespace std;
dann
std :: cout << std :: fixed << std :: setprecision (2) << d;
wird vereinfacht
cout << fixed << setprecision (2) << d;
using namespace std;- verstehen Sie, warum Sie dies tun.
Dies ist ein Beispiel mit einer Matrix.
cout<<setprecision(4)<<fixed<<m[i][j]