Qt四舍五入取模取余操作

来源:互联网 发布:阿里云为什么要装linux 编辑:程序博客网 时间:2024/05/16 10:13
 

Qt四舍五入取模取余操作

Qt中有两个函数进行四舍五入操作:qRound与qRound64。

qint64 qRound64 ( qreal value )
将qreal类型的数值,四舍五入后返回一个最近64位的整数,示例:

qreal valueA = 42949672960.3;
qreal valueB = 42949672960.7;

int roundedValueA = qRound(valueA);
// roundedValueA = 42949672960
int roundedValueB = qRound(valueB);
// roundedValueB = 42949672961

 

int qRound ( qreal value )
四舍五入返回最近的整数值,示例:

qreal valueA = 2.3;
qreal valueB = 2.7;

int roundedValueA = qRound(valueA);
// roundedValueA = 2
int roundedValueB = qRound(valueB);
// roundedValueB = 3

 

 

上面四舍五入的进位基数是1,如果基数不等于1,就要自己写Round函数。

int getRoung(int iValue, int iBase)
{
    int iFactor,int iMod;
    iFactor = iValue / iBase;
    iMod = iValue % iBase;
    iFactor=(iMod*2>iBase)?(iFactor+1):iFactor;
    int iRtn=iFactor*iBase;
    return iRtn;
}

 

 

返回网格内距离一个给定点最近的网格点。

围着点做一个矩形,找出在矩形内的点。然后计算距离,找距离最近的点。

原创粉丝点击