69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
double roundm(double f,int n)
{
double t1,t2;
t1= f* pow(10.0,1.0*n);
t2= t1-floor(t1);
t1=floor(t1);
if (t2>=0.1)
t1+=1.0;
t1= t1 / pow(10.0, n);
return t1;
}
int main()
{
double x;
x=6.0068003; printf("roundm(%.12f,%d)=%.12f\n",x,6,roundm(x,6));
x=6.0068008; printf("roundm(%.12f,%d)=%.12f\n",x,6,roundm(x,6));
x=6.0068000; printf("roundm(%.12f,%d)=%.12f\n",x,6,roundm(x,6));
}
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double roundm(double *amt, int n)
{
// (int)(*amt * pow(10.0, n + 1)) % 10 == 0 ? *amt = (int)(*amt * pow(10.0, n)) / pow(10.0, n) : *amt = (((int )*amt * pow(10.0, n)) + 1) / pow(10.0, n);
if ((int)(*amt * pow(10.0, n + 1)) % 10 == 0)
{
*amt = (int)(*amt * pow(10.0, n)) / pow(10.0, n);
}
else
{
*amt = (((int )(*amt * pow(10.0, n))) + 1) / pow(10.0, n);//少加了个括号
}
return *amt;
}
int main()
{
double amt = 6.0068003;
roundm(&amt, 6);
printf("%f\n", amt);
return 0;
}
~
//Round(1.234,2) = 1.23
//Round(1.234,0) = 1.0
//Round(123.4,-1) = 120.0
double Round(double dVal, short iPlaces) {
double dRetval;
double dMod = 0.0000001;
if (dVal<0.0) dMod=-0.0000001;
dRetval=dVal;
dRetval+=(5.0/pow(10.0,iPlaces+1.0));
dRetval*=pow(10.0,iPlaces);
dRetval=floor(dRetval+dMod);
dRetval/=pow(10.0,iPlaces);
return(dRetval);
}
double round(double dVal, short iPlaces) //iPlaces>=0
{
unsigned char s[20];
double dRetval;
sprintf(s,"%.*lf",iPlaces,dVal);
sscanf(s,"%lf",&dRetval);
return (dRetval);
}
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double roundm(double *amt, int n)
{
if ((int)(*amt * pow(10.0, n + 1)) % 10 == 0)
{
*amt = (int)(*amt * pow(10.0, n)) / pow(10.0, n);
}
else
{
*amt = (((int )*amt * pow(10.0, n)) + 1) / pow(10.0, n);
}
return *amt;
}
int main()
{
double amt = 6.0068003;
roundm(&amt, 6);
printf("%f\n", amt);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
double roundm(double f,int n)
{
f+= 0.5* pow(10.0,-1.0 * n);
f= floor( f * pow(10.0,n));
f= f / pow(10.0, n);
return f;
}
int main()
{
double f=5.2345678934;
printf("roundm(%.12f,%d)=%.12f\n",f,1,roundm(f,1));
printf("roundm(%.12f,%d)=%.12f\n",f,2,roundm(f,2));
printf("roundm(%.12f,%d)=%.12f\n",f,3,roundm(f,3));
printf("roundm(%.12f,%d)=%.12f\n",f,4,roundm(f,4));
printf("roundm(%.12f,%d)=%.12f\n",f,5,roundm(f,5));
printf("roundm(%.12f,%d)=%.12f\n",f,6,roundm(f,6));
printf("roundm(%.12f,%d)=%.12f\n",f,7,roundm(f,7));
}