Het juiste aantal decimalen afdrukken met cout

Ik heb een lijst met float-waarden en ik wil ze afdrukken met coutmet 2 decimalen.

Bijvoorbeeld:

10.900  should be printed as 10.90
1.000 should be printed as 1.00
122.345 should be printed as 122.34

Hoe kan ik dit doen?

( setprecisionlijkt hier niet bij te helpen.)


Antwoord 1, autoriteit 100%

Met <iomanip>kunt u gebruiken std::fixeden std::setprecision

Hier is een voorbeeld

#include <iostream>
#include <iomanip>
int main()
{
    double d = 122.345;
    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    std::cout << d;
}

En je krijgt output

122.34

Antwoord 2, autoriteit 19%

Je was er bijna, moet ook std::fixed gebruiken, raadpleeg 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;
}

uitgangen:

0.12
1.23
12.35
123.45
1234.50
12345.00

Antwoord 3, autoriteit 8%

setprecision(n)is van toepassing op het gehele getal, niet op het fractionele deel. U moet het fixed-point formaat gebruiken om het van toepassing te laten zijn op het fractionele deel: setiosflags(ios::fixed)


Antwoord 4, autoriteit 6%

Vereenvoudig het geaccepteerde antwoord

Vereenvoudigd voorbeeld:

#include <iostream>
#include <iomanip>
int main()
{
    double d = 122.345;
    std::cout << std::fixed << std::setprecision(2) << d;
}

En je krijgt output

122.34

Referentie:


Antwoord 5, autoriteit 4%

Ik had een probleem met gehele getallen terwijl ik een consistente opmaak wilde.

Een herschrijving voor de volledigheid:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    //    floating point formatting example
    cout << fixed << setprecision(2) << 122.345 << endl;
    //    Output:  122.34
    //    integer formatting example
    cout << fixed << setprecision(2) << double(122) << endl;
    //    Output:  122.00
}

Antwoord 6, autoriteit 2%

#include<stdio.h>
int main()
{
 double d=15.6464545347;
printf("%0.2lf",d);
}

Antwoord 7

Ik had dit soortgelijk probleem in een codeerwedstrijd en dit is hoe ik het heb aangepakt.
Een precisie van 2 instellen op alle dubbele waarden

Eerst de koptekst toevoegen om setprecision te gebruiken

#include <iomanip>

Vervolgens het toevoegen van de volgende code in onze hoofd

 double answer=5.9999;
  double answer2=5.0000;
  cout<<setprecision(2)<<fixed;
  cout <<answer << endl;
  cout <<answer2 << endl;

Uitvoer:

5.99
5.00

Je moet vast gebruiken om 5,00 te schrijven, daarom komt je output niet voor 5,00.

Ik voeg een korte referentievideolink toe die nuttig is


Antwoord 8

Je moet de ‘zwevende modus’ op vast zetten.

float num = 15.839;
// this will output 15.84
std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl;

Antwoord 9

Om vaste 2 cijfers achter de komma in te stellen, gebruikt u eerst deze:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

Druk vervolgens uw dubbele waarden af.

Dit is een voorbeeld:

#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;
}

Antwoord 10

met sjablonen

#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;
}

ook vergelijkbaar voor wetenschappelijke, met een breedte-optie ook (handig voor kolommen)

// 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';
}

Antwoord 11

#include <iostream>
#include <cstdio>
#include <iomanip>
using namespace std;
int main() {
    int a;
    long int b;
    char c;
    float d;
    double e;
    cin>>a>>b>>c>>d>>e;
    cout<<a<<"\n"<<b<<"\n"<<c<<"\n";
    cout<<fixed<<setprecision(3)<<d<<"\n";
    cout<<fixed<<setprecision(9)<<e;
    return 0;
}

Eenvoudige code zou je zeker helpen!


Antwoord 12

Slechts een klein punt; zet het volgende in de kop

namespace std gebruiken;

dan

standaard::cout << standaard::vast << std::setprecision(2) << d;

wordt vereenvoudigd tot

cout << vast << setprecisie(2) << d;


Antwoord 13

dit is een voorbeeld met een matrix.

cout<<setprecision(4)<<fixed<<m[i][j]

LEAVE A REPLY

Please enter your comment!
Please enter your name here

11 + 18 =

Other episodes