IT story

cout을 사용하여 정확한 소수점 수 인쇄

hot-time 2020. 8. 11. 18:56
반응형

cout을 사용하여 정확한 소수점 수 인쇄


float목록이 있고 cout소수점 이하 두 자리 로 인쇄하고 싶습니다 .

예를 들면 :

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

어떻게 할 수 있습니까?

( setprecision이것이 도움이되지 않는 것 같습니다.)


로에게 <iomanip>, 당신이 사용할 수있는 std::fixedstd::setprecision

다음은 예입니다.

#include <iostream>
#include <iomanip>

int main()
{
    double d = 122.345;

    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    std::cout << d;
}

그리고 당신은 출력을 얻을 것입니다

122.34

거의 다 왔으니 std :: fixed를 사용해야합니다. 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;
}

출력 :

0.12
1.23
12.35
123.45
1234.50
12345.00

setprecision(n)소수 부분이 아닌 전체 숫자에 적용됩니다. 분수 부분에 적용하려면 고정 소수점 형식을 사용해야합니다.setiosflags(ios::fixed)


허용되는 답변을 단순화

단순화 된 예 :

#include <iostream>
#include <iomanip>

int main()
{
    double d = 122.345;
    std::cout << std::fixed << std::setprecision(2) << d;
}

그리고 당신은 출력을 얻을 것입니다

122.34

참고:


일관된 서식을 원하면서 정수 문제가 발생했습니다.

완전성을위한 재 작성 :

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

'플로트 모드'를 고정으로 설정해야합니다.

float num = 15.839;

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

I had this similar problem in a coding competition and this is how I handled it. Setting a precision of 2 to all double values

First adding the header to use setprecision

#include <iomanip>

Then adding the following code in our main

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

Output:

5.99
5.00

You need to use fixed for writing 5.00 thats why,your output won't come for 5.00.

A short reference video link I'm adding which is helpful


To set fixed 2 digits after the decimal point use these first:

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

Then print your double values.

This is an example:

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

#include<stdio.h>
int main()

{

 double d=15.6464545347;

printf("%0.2lf",d);

}

Just a minor point; put the following in the header

using namespace std;

then

std::cout << std::fixed << std::setprecision(2) << d;

becomes simplified to

cout << fixed << setprecision(2) << d;


this an example using a matrix.

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

참고URL : https://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout

반응형