coutを使用して正しい小数点数を印刷する


133

float値のリストがあり、cout小数点以下2桁で印刷したいと思います。

例えば:

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は役に立たないようです。)

回答:


195

では<iomanip>、あなたが使用することができますstd::fixedし、std::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

6
プログラムで「std:fixed」を使用する理由
Vilas Joshi

1
有用なヘッダは、このために定義することができる#define FIXED_FLOAT(x) std::fixed <<std::setprecision(2)<<(x) に使用を簡素化している:cout<<FIXED_FLOAT(d)
Udayrajデシュムク

12
@ VilasJoshi、setprecisionは、小数点以下の桁数を設定します。5桁あり、setprecision(2)を使用すると、2桁が表示されますが、0桁の場合は何も表示されません。図5は、5.00ない5として表現されるように示される
vaibnak

43

あなたはほとんどそこにいたので、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

18

setprecision(n)小数部ではなく整数全体に適用されます。小数部に適用するには、固定小数点形式を使用する必要があります。setiosflags(ios::fixed)


12

受け入れられた答えを簡素化する

簡略化した例:

#include <iostream>
#include <iomanip>

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

そして、あなたは出力を取得します

122.34

参照:


これは私にとってはうまくいきました:std :: cout << std :: setprecision(2)<< std :: fixed << d;
Andrea Girardi

5

一貫した書式設定が必要なときに、整数に関する問題が発生しました。

完全性のための書き直し:

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

名前空間を使用していないため、std ::がcoutおよびendlの直前にありません。
blackforest-tom

@ blackforest-tom-多分あなたは正しい、思い出せない---私は通常、作業プログラムをコピーして貼り付けるので、これはVisual Studioまたは他の場所でそのまま実行された可能性があります。
Manohar Reddy Poreddy

3

コーディングコンテストでも同様の問題がありました。すべてのdouble値に精度2を設定する

最初にヘッダーを追加してsetprecisionを使用する

#include <iomanip>

次に、メインに次のコードを追加します

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

出力:

5.99
5.00

5.00を書き込むにはfixedを使用する必要があります。そのため、出力は5.00になりません。

追加する短い参照ビデオリンクは役に立ちます


2

「フロートモード」を固定に設定する必要があります。

float num = 15.839;

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

1

小数点の後に固定された2桁を設定するには、これらを最初に使用します。

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

次に、double値を出力します。

これは例です:

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

1
#include<stdio.h>
int main()

{

 double d=15.6464545347;

printf("%0.2lf",d);

}

0

テンプレート付き

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

科学の場合も同様で、幅のオプションもあります(列に役立ちます)

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

-3

ほんのマイナーなポイントです。ヘッダーに以下を入れてください

名前空間stdを使用します。

その後

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

単純化される

cout <<固定<< setprecision(2)<< d;


2
はい、それは「単純化」されますが、これは強くお勧めできません。using namespace std;そのために使用しないでください-あなたがそうしている理由を理解してください。
カルロスF

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.