回答:
では<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
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(2)<<(x) に使用を簡素化している:cout<<FIXED_FLOAT(d)
あなたはほとんどそこにいたので、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
受け入れられた答えを簡素化する
簡略化した例:
#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
}
コーディングコンテストでも同様の問題がありました。すべての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桁を設定するには、これらを最初に使用します。
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;
}
テンプレート付き
#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';
}
これはマトリックスを使用した例です。
cout<<setprecision(4)<<fixed<<m[i][j]