之前用double转string 均采用非基础库,但是仔细一想,基础库也并非不能实现,种种原因驱使之下,之下代码诞生,不可能有人看不懂!
必须要批判一下CSDN上面,double转字符串 都还要上传并且消耗积分下载的人,真的抽象
#include "stdio.h"
//获取字符串长度
int strlen(char * ret)
{
int i = 0;
while (true)
{
if (*(ret + i) == '\0')
{
break;
}
else
{
i += 1;
}
}
return i;
}
void doubletochar(char *strdouble,double result)
{
//char strdouble[99];
double ret = result;
int zheng = (int)result, zweishu = 0, wei = 10, xweishu = 0;
while (true)
{
int yushu = zheng % wei;
if (yushu == zheng)
{
zweishu++;
break;
}
else
{
zweishu++;
wei = wei * 10;
}
}
for (int i = 0; i < zweishu; i++)
{
int yushu = zheng % 10;
zheng = zheng / 10;
strdouble[zweishu - i - 1] = yushu + '0';
}
strdouble[zweishu] = '.';
while (true)
{
if ((int)ret != ret)
{
ret = ret * 10;
xweishu++;
}
else
break;
}
for (int i = 0; i < xweishu; i++)
{
int yushu = (int)ret % 10;
ret = ret / 10;
strdouble[zweishu + xweishu - i] = yushu + '0';
}
strdouble[zweishu + xweishu + 1] = '\0';
for (int i = 0; i < strlen(strdouble); i++)
{
//putchar(strdouble[i]);
}
}
int main()
{
char demo[99];
doubletochar(demo, 18.369);
printf("%s", demo);
while (true);
}