私は setw と setprecision 関数を学んでいるので、これまで試してきたことと、いくつか質問があります。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float y = 1.25;
cout << fixed << setw(10) << setprecision(2) << y << endl;
cout << "\n\n\nx\n";
float x = 213565544.4826;
cout << fixed << setw(13) << setprecision(3) << x << endl;
cout << fixed << setw(14) << setprecision(3) << x << endl;
cout << fixed << setw(15) << setprecision(3) << x << endl;
cout << fixed << setprecision(3) << x;
cout << "\n\n\nz\n";
float z = 213565544.4826;
cout << setw(11) << setprecision(1) << x << endl;
cout << fixed << setw(12) << setprecision(1) << x << endl;
cout << fixed << setw(11) << setprecision(1) << x << endl;
cout << setw(12) << setprecision(1) << x << endl;
cout << "\n\n\nm\n";
float m = -344.275;
cout << fixed << setprecision(1) << x << endl;
cout << fixed << setw(8) << setprecision(1) << x << endl;
cout << fixed << setw(7) << setprecision(1) << x << endl;
cout << fixed << setw(6) << setprecision(1) << x << endl;
return 0;
}
入力は次のとおりです。
1.25
x
213565552.000
213565552.000
213565552.000
213565552.000
z
213565552.0
213565552.0
213565552.0
213565552.0
m
213565552.0
213565552.0
213565552.0
213565552.0
だから、今私の質問は:
1) そもそもなぜ「固定」を使うのですか?
この例を見ると:
cout << setw(11) << setprecision(1) << x << endl;
cout << fixed << setw(11) << setprecision(1) << x << endl;
それらは同じ値を出力するので、fixed は実際に何が変わるのでしょうか?
2) setw は負の数に対してどのように機能しますか?
mの最後の例では。結果はすべての例で同じですが、 - 記号は setw で何を変更しますか?
213565552.0
213565552.0
213565552.0
213565552.0
これらの数字はどこから来たのですか?m の値は、出力されたものとはまったく異なります。
3) を行います。は1位と数えますか?
たとえば、番号 1.23 と setw(10) があります。
前に 6 個のスペースがあり、次に 1.23 になります (ドットが 1 としてカウントされるため)。本当?
4) setprecision が setw と一緒に使用されるのはなぜですか? 使用されていないのに 0000 が表示されるのはなぜですか? float が処理できる数の 0 が表示されますか?
5) なぜ x の値は
213565552.000
213565552.000
213565552.000
213565552.000
x = 213565544.4826 の場合。
数字 44.4826 はどこで失われますか?