/*******************************************
函数名称:Numtostr
功 能:任意数转字符串
参 数:fNum:要显示的数字
pBuf:保存的字符串数组
nSize:最大显示长度
Point_bit:小数点位数
返回值 :无
********************************************/
void Numtostr(unsigned char *pBuf, unsigned char nSize, float fNum,unsigned char Point_bit )
{
unsigned char i=0;
unsigned char cursor = 0 ;
long fNum_temp = 0 ;
unsigned char Start_Addr=0; //起始地址
if(Point_bit!=0)
{
float temp=5; //四舍五入补偿
for(i=(Point_bit+1);i>0;i--)
{
temp/=10;
}
if(fNum > 0) //正数
{
fNum+=temp;
}
else if(fNum < 0) //负数
{
fNum-=temp;
}
}
if (fNum < 0) //如果是负数先转换为正数,添加负号
{
fNum = 0 - fNum ;
pBuf[cursor++]= '-';
Start_Addr=1;
}
fNum_temp = (long)fNum ; //获取整数
if(fNum_temp>=1) //1以上的
{
while (fNum_temp) //取整数各位
{
pBuf[cursor++] = (fNum_temp%10)+'0';
fNum_temp /= 10 ;
}
char c ;
for(i=Start_Addr; i<((cursor+Start_Addr)/2);i++)//转换高低位
{
c = pBuf[i];
pBuf[i] = pBuf[cursor-1-(i-Start_Addr)];
pBuf[cursor-1-(i-Start_Addr)] = c ;
}
}
else //0.xxxxx的
{
pBuf[cursor++]='0'; //第一位为0
}
if((cursor+1)>=nSize) //如果超过显示位数或整数位+1等于最大显示位
{
pBuf[cursor] = '\0' ; //小数点不显示,即整数
}
else
{
if(Point_bit!=0)
{
pBuf[cursor++] = '.' ; //显示小数点
fNum =fNum-(long)fNum; //得到小数部分
i = nSize-cursor ;
Point_bit =((char)Point_bit - (char)i) > 0 ? i : Point_bit;
while (Point_bit) //获取小数,
{
fNum *= 10 ;
pBuf[cursor++] = (unsigned char)fNum+'0';//得到小数各位
Point_bit-- ;
fNum -=(float)((unsigned char)fNum);
}
}
pBuf[cursor] = '\0' ; //结束标识符 ;
}
pBuf[nSize] = '\0' ; //结束标识符
}
|