2017-05-21 17 views
0

QTextStreamを使用してファイルにデータを書き込みます。同様に、上記QTextStreamはドット(小数点記号)で整列します

stream << qSetFieldWidth(5) << i 
      << qSetFieldWidth(12) << t 
      << qSetFieldWidth(12) << y 
      << qSetFieldWidth(12) << yy 
      << endl; 

しかし、私はドット(小数点区切り)によって整列列を希望することにより製造される

i   t   y   yy   
0 0.0166667 -0.649999  67.6666   
1 0.0333333 0.477777 -43.4444   
2  0.05 -0.246295  30.6295   
3 0.0666666 0.264197  -18.753   
4 0.0833333 -0.0483533  14.1687   
5   0.1 0.187791  -7.7791   
6 0.116667 0.0581394  6.85273   
7 0.133333 0.172351 -2.90181   
8  0.15 0.123988  3.60121   
9 0.166667 0.184008 -0.734136 

0 0.0166667 -0.649999  67.6666   
1 0.0333333 0.477777 -43.4444   
2 0.05  -0.246295  30.6295   
3 0.0666666 0.264197 -18.753   
4 0.0833333 -0.0483533 14.1687   
5 0.1   0.187791  -7.7791   
6 0.116667  0.0581394  6.85273   
7 0.133333  0.172351  -2.90181   
8 0.15   0.123988  3.60121   
9 0.166667  0.184008  -0.734136 
それは素敵なように(ほぼ完璧な)編成されています

どうすればよいですか?

答えて

1

数値を整数(右揃え)と小数部分(左揃え)に分割する必要があります(小数点の前にあるゼロを取り除きます)。

より簡単な方法は、出力を整数表現の桁数に応じたスペースで埋め込むことです(符号も考慮してください)。テストされていない

// inefficient, but illustrates the concept: 
int NumIntDig(double x) { 
    stringstream s; 
    s << int(x); 
    return s.str().size(); 
} 

stream << qSetFieldAlignment(AlignRight) << qSetFieldWidth(5) << i 
     << qSetFieldAlignment(AlignLeft) << 
     << qSetFieldWidth(4-NumIntDig(t)) << " " 
     << qSetFieldWidth(8+NumIntDig(t)) << t 
     << qSetFieldWidth(4-NumIntDig(y)) << " " 
     << qSetFieldWidth(8+NumIntDig(t)) << y 
     << qSetFieldWidth(4-NumIntDig(yy)) << " " 
     << qSetFieldWidth(8+NumIntDig(t)) << yy 
     << endl; 
関連する問題