0

私は丸め機能を持っています、

float myround(float x, int places)
{
   float const shift = powf(10.0f, places);
   x *= shift;
   x = floorf(x + 0.5f);
   x /= shift;

   return x;
}

数値を四捨五入して小数点以下4桁に丸めようとすると、数値を次のように出力します

printf("%f ", x); 

四捨五入せずに数値を取得します。で印刷すると

printf("%.4f ", x);

数値は 4 桁に丸められます。最初の printf は、数値を既に四捨五入しているので、数値を小数点以下 4 桁まで出力すべきではありませんか?

ありがとう。

4

2 に答える 2

2

フロートを丸めることはできません。特定の精度でのみ印刷できます。すべてのフロートは常に「丸められていません」ですが、値を変更して丸められた量に近づけることができます。

于 2013-05-20T22:00:29.267 に答える
0

からman printf

f, F   The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision
       specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point  appears,  at  least one
       digit appears before it.

The fprintf(), printf(), sprintf(), vprintf(), vfprintf(), and vsprintf() functions conform to C89 and C99.

したがって、いいえ、指定しない場合、精度として 6 を使用する以外は何もすべきではありません。

于 2013-05-20T22:09:39.840 に答える