函数是将一个float数拆分到一个数组中,
- #include "stdio.h"
- //#include "reg51.h"
- #include "math.h"
- //拆字函数
- //入口变量ft ,n浮点数据,小数点保留位数
- //传出参数 m,字符位数
- unsigned char tempd[20]={0,0,0,0,0,0,0,0,0,0,0,0};
- unsigned char SpeaData(float ft,unsigned char n)
- {
- unsigned char i=0;
- unsigned long int temp=0;
- double t;
- unsigned char m=0,ss=48;
-
- //for(i=0;i<n;i++)
- //ft=ft*pow(10,n);
- //拆分整数部分
- if(ft<0)
- {
- tempd[m++]='-';
- ft=0-ft;
- temp=(unsigned long int)ft;
- }
- for(i=0;pow(10,i)<temp;i++);
- do
- {
- t=pow(10,i-1);
- t=(double)temp/t;
- t=(unsigned int)t%10;
- tempd[m++]=(unsigned char)t+48;
- //tempd[m++]=(unsigned char)((unsigned int)(temp/(unsigned long int)pow(10,i-1))%10);
- i--;
- }while(i);
- //拆分小数部分
- if(n)
- {
- tempd[m++]='.';
- ft=ft-(long int)ft;
- ft=ft*pow(10,n);
- temp=(unsigned long int)ft;
- do
- {
- t=pow(10,n-1);
- t=(double)temp/t;
- t=(unsigned int)t%10;
- tempd[m++]=(unsigned char)t+48;
- //tempd[m++]=(unsigned char)((unsigned int)(temp/(unsigned long int)pow(10,i-1))%10);
- n--;
- }while(n);
- }
- return m;
- }
- main()
- {
- unsigned char n=SpeaData(-9999.12345,4);
- while(1);
- }
复制代码运行发现有时候又乱码,不知道是什么地方的问题,例如我现在输入的9999.12345.拆完之后小数后面就会有乱码出现。求解!
一周热门 更多>