C/C++ double 转 字符串string (char 数组)

之前用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);
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值