😸

[C] round 関数

に公開

機能

小数値を四捨五入して最も近い整数値にする標準関数[1]

使用例

3.0 と表示する

\n は改行を表す エスケープシーケンス[2]

コード

printf("%f\n", round(2.7));
全文
#include <stdio.h>
#include <math.h>
void main() {
    printf("%f\n", round(2.7));
}

実行結果

3.000000

-3.0 を変数から表示する

コード

double x = -2.6;
printf("%f\n", round(x));
全文
#include <stdio.h>
#include <math.h>
void main() {
    double x = -2.6;
    printf("%f\n", round(x));
}

実行結果

-3.000000
脚注
  1. C99で追加された標準数学関数。整数型ではなくdouble型を返す点に注意。 ↩︎

  2. 改行やタブなど、画面に表示されない制御文字のこと。 ↩︎

Discussion