四捨五入

下桁数を指定した四捨五入を行うプログラムです。

四捨五入list_30.c)
#include <stdio.h>
#include <math.h>

int rintv(double v)
{
    if(v > 0) return (int)(v+0.5);
    else      return (int)(v-0.5);
}
double round_off(int dec_p,double value)
{
    int intv = rint(pow(10,dec_p) * value);
    return intv / pow(10,dec_p);
}

int main()
{
    double v=10./3.;
    printf("value = %lf\n",round_off(2,v));
    return 0;
}
実行結果
Gami[1499]% list_30.exe
value = 3.330000
Gami[1500]%
inserted by FC2 system