c语言如何将字母转换成实数,c语言将浮点型数据转换成字符串

在程序中,可能会遇到需要将浮点型的数据转换成字符串:#include

void float2char(float,char*,int);

int main()

{

char buffer[10];

float2char(123.4567,buffer,10);

printf("%f 转换成字符串 %s\n",123.4567,buffer);

float2char(-654.321,buffer,10);

printf("%f 转换成字符串 %s\n",-654.321,buffer);

return 0;

}

void float2char(float slope,char*buffer,int n)  //浮点型数,存储的字符数组,字符数组的长度

{

int temp,i,j;

if(slope>=0)//判断是否大于0

buffer[0] = '+';

else

{

buffer[0] = '-';

slope = -slope;

}

temp = (int)slope;//取整数部分

for(i=0;temp!=0;i++)//计算整数部分的位数

temp /=10;

temp =(int)slope;

for(j=i;j>0;j--)//将整数部分转换成字符串型

{

buffer[j] = temp%10+'0';

temp /=10;

}

buffer[i+1] = '.';

slope -=(int)slope;

for(i=i+2;i

{

slope*=10;

buffer[i]=(int)slope+'0';

slope-=(int)slope;

}

buffer[n-1] = '\0';

}

e4efc1990cb9d6a9f0fb1d61cfc9e043.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值