首页 > 代码库 > C语言下double转char*或者std::string,可以精确转换不含多余的0

C语言下double转char*或者std::string,可以精确转换不含多余的0

char* GetDoubleStr(double value)
{
	char buf[32]={0};//长度可以自定义
	sprintf(buf,"%.8f",value);//保留8位小数,不够补0
	int index = 0;
	int len = strlen(buf);
	for(int i = len-1;i>0;i--)
	{
		if(buf[i] == ‘0‘)
			continue;
		else
		{
			if(buf[i] == ‘.‘) index = i;			
			else index = i+1;
			break;
		}
	}
	buf[index] = ‘\0‘;
	return buf;
}

  

C语言下double转char*或者std::string,可以精确转换不含多余的0