题目描述
请编写函数,对小数进行四舍五入。
double Round(double x, int d);
说明:x 为任意实数,d 为小位位数。函数值为将 x 其四舍五入后得到的保留 d 位小数的实数。裁判程序
#include <stdio.h>
#include <math.h>
int IsOdd(int number);
int IsEven(int number);
double Power(double x, int n);
double Round(double x, int d);
int main()
{
double x;
int d;
scanf("%lg%d", &x, &d);
printf("%.15g\n", Round(x, d));
return 0;
}
...(略)...
/* 你提交的代码将被嵌在这里 */
输入样例1:
25.836 2
输出样例1:
25.84
输入样例2:
-18.274 2
输出样例2:
-18.27
源代码
double Round(double x, int d)
{
int flag=0,i;
double muti,current;
long long int part;
if(x<0)
{
current=-x;
flag=1;
}
else
current=x;
if(d>=0)
{
current=current*Power(10.0,d+1)+5;
part =current/10;
current=part;
current=current/(Power(10.0,d));
if(flag==1)
printf("-");
return current;
}
if(d<0)
{
part=current;
part=part/(Power(10.0,-d-1))+5;
part=part/10;
part=part*Power(10.0,-d);
if(flag==1)
printf("-");
return part;
}
}