请问有没有现成的把浮点四舍五入转换成整数的函数?

布鲁克零三四四 2002-07-25 04:13:20
请问有没有现成的把浮点四舍五入转换成整数的函数?
...全文
480 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
fatalerror99 2002-08-04
  • 打赏
  • 举报
回复
ceil( x + 0.5 )
floor( x + 0.5 )
网事随逢 2002-08-04
  • 打赏
  • 举报
回复
这个问题好像还没有结贴:我现在也有点问题不放在加几句
1.1.234567--->1.234
1.234567--->1.235
1.29----->1.2
1.29----->1.3
2.向上入,向下入,入后的小数位数可以选择(只是向上,下取整,没有四舍五入)
double myCeilF(double myNum,int i,int j)
{
double beforeCeil,middleCeil0,middleCeil1,afterCeil;
if(j==0)//j有两种值可选择j=0代表向下取
{
beforeCeil=myNum;
//i代表你所要取到小数点后的位数
middleCeil0=beforeCeil*pow(10,i);
middleCeil1=ceil(middleCeil);
afterCeil=middleCeil1/pow(10,i);
return afterCeil1;
}
else if(j==1)//j有两种值可选择j=1代表向上取
{
beforeCeil=myNum;
//i代表你所要取到小数点后的位数
middleCeil0=beforeCeil*pow(10,i);
middleCeil1=floor(middleCeil);
afterCeil=middleCeil1/pow(10,i);
return afterCeil1;
}
else
{
printf("wrong input parameter!");
}
}
3.(四舍五入)double mySSWR(double myNum)
{
double afterSSWR;
char buf[1024]="";
sprintf(buf,"%.3f",myNum);
afterSSWR=atof(buf);
return afterSSWR;
}
以上只能实现限定位数(3)的四舍五入,不知道有什么好的方法可以实现配置。
以上需要头文件:#include <stdio.h>
#include <math.h>
#include <stdlib.h>
并且在borland bcc32编译器上编译通过,不知道在ASCII C上可不可以,
希望高手能出来指点一下。





zheng_can 2002-07-25
  • 打赏
  • 举报
回复
嗯…………
好像都说得差不多了:)
alexxing 2002-07-25
  • 打赏
  • 举报
回复
嗨,temp没赋初值:
float f(float i,int n)
{
float temp, k = pow(10,n);
temp=i*k; //先放大
temp=floor(temp+0.5); //圆整
temp/=k; //再缩小
return temp;
}
alexxing 2002-07-25
  • 打赏
  • 举报
回复
不过他把乘/除的顺序,还有floor的位置都搞错了:

float f(float i,int n)
{
float temp, k = pow(10,n);
temp*=k; //先放大
temp=floor(temp+0.5); //圆整
temp/=k; //再缩小
return temp;
}
alexxing 2002-07-25
  • 打赏
  • 举报
回复
楼上的楼上的有没有搞错,用起了字符串操作
这么简单的问题用template,在C语言中怎么办?
如果不是圆整为整数,而是到小数点后某一位,siegf(nowhy!) 的pow()是正解
studentwuz 2002-07-25
  • 打赏
  • 举报
回复
ceil()
floor()
anrxhzh 2002-07-25
  • 打赏
  • 举报
回复
#include <iostream>
#include <sstream>
#include <limits>
using namespace std;

#undef max
#undef min
template <typename T> inline
const T& max(const T& a, const T& b)
{ return a < b ? b : a; }

template <typename T> inline
const T& min(const T& a, const T& b)
{ return a > b ? b : a; }

template<typename T>
T SetPrecision(T num,unsigned int pre,bool round)
//功能:浮点数精度转换
//参数:num[in]: 待转换的浮点数
// pre[in]:需保留的小数点位数
// round[in]:是否四舍五入
//返回:转换后的浮点数
{
stringstream s;
pre = min(pre,static_cast<unsigned int>(numeric_limits<T>::digits10));
s.precision(round?pre:numeric_limits<T>::digits10 );
s << fixed << num;
if(!round)
{
string str(s.str());
int dot(str.find('.'));
if(dot>=0)
{
str.erase(str.begin()+dot+pre+1,str.end());
s.str(str);
}
}
s >> num;
return num;
}

int main()
{
cout << SetPrecision(3.1415,3,true) << endl;
cout << SetPrecision(3.1415,3,false) << endl;
}

//output:
//3.142
//3.141

alexxing 2002-07-25
  • 打赏
  • 举报
回复
上面的XXX宏是丑了点:-)
关键是只能用于变量,我传一个常量怎么办?而且修改了x的值,可能在后面造成错误

#define round(x) ( (long) ( (x) < 0 ? ((x) - 0.5) : ((x) + 0.5) ) )

最好用 inline

inline long round(double x)
{
return (long) ( x < 0 ? (x - 0.5) : (x + 0.5) );
}
lx_cyh 2002-07-25
  • 打赏
  • 举报
回复
ceil floor两个函数分别为下取整和上取整

ceil: rounds up

double ceil(double x);

Prototype in math.h

Returns the smallest integer greater
than or equal to x.

See also floor


tigerfox 2002-07-25
  • 打赏
  • 举报
回复
根据你要求精度自编一个吧
siegf 2002-07-25
  • 打赏
  • 举报
回复
long f(float i)
{
int temp;
remp=floor(i+0.5);
return;
}

如果是把小数的第n位四舍五入

float f(float i,int n)
{
float temp;
temp/=pow(10,n);
temp+=0.5;
temp*=pow(10,n);
temp=floor(temp);
return temp;
}
winco 2002-07-25
  • 打赏
  • 举报
回复
同意楼上的说法;
symbole 2002-07-25
  • 打赏
  • 举报
回复
#define XXX(x) if((x) > 0.0) x += 0.5; \
else if((x) < 0.0) x -= 0.5; \
else (x) =0; \
/******/
double x0 ;
int x1 ;

x0 = .....

x1 = (int) XXX(x0)

长得是丑了点,可是简单,有效啊!
fangrk 2002-07-25
  • 打赏
  • 举报
回复
如果是简单的n = (int)(db+0.5);
db=-0.8;
n=(int)(-0.3)=0;
fangrk 2002-07-25
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
int sswu(float);
void main()
{ float f;
cin>>f;
cout<<sswu(f);
}
int sswu(float f)
{ bool minus=(f<0.0? true:false);
if(minus) f*=-1;
int a=(int)f;
if( (f-a)>=0.5 ) ++a;
if(minus) a*=-1;
return a;
}
fangrk 2002-07-25
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
int sswu(float);
void main()
{ float f;
cin>>f;
cout<<sswu(f);
}
int sswu(float f)
{ bool minus=(f<0.0? true:false);
if(minus) f*=-1;
int a=(int)f;
if( (f-a)>=0.5 ) ++a;
if(minus) a*=-1;
return a;
}
sans 2002-07-25
  • 打赏
  • 举报
回复
CString str;
str.Format( "%d", db );
n = atoi( str );
wistaria 2002-07-25
  • 打赏
  • 举报
回复
double db;
int n;
n = (int)(db+0.5);

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧