C double转char字符串

找个double转char字符串的代码,国内搜出来的都是些什么狗屎

翻墙出去,一搜就有几种不错的方法

方法1:使用sprintf

https://cboard.cprogramming.com/c-programming/38507-double-string-conversion.html 

double d = 123456.1234567899;
char s[50];
 
sprintf(s,"%f", d);
printf("%s\n", s);
 
sprintf(s,"%.10f", d);
printf("%s\n", s);
 
sprintf(s,"%.2f", d);
printf("%s\n", s);

方法2:自己写

https://www.geeksforgeeks.org/convert-floating-point-number-string/

// C program for implementation of ftoa() 
#include<stdio.h> 
#include<math.h> 

// reverses a string 'str' of length 'len' 
void reverse(char *str, int len) 
{ 
	int i=0, j=len-1, temp; 
	while (i<j) 
	{ 
		temp = str[i]; 
		str[i] = str[j]; 
		str[j] = temp; 
		i++; j--; 
	} 
} 

// Converts a given integer x to string str[]. d is the number 
// of digits required in output. If d is more than the number 
// of digits in x, then 0s are added at the beginning. 
int intToStr(int x, char str[], int d) 
{ 
	int i = 0; 
	while (x) 
	{ 
		str[i++] = (x%10) + '0'; 
		x = x/10; 
	} 

	// If number of digits required is more, then 
	// add 0s at the beginning 
	while (i < d) 
		str[i++] = '0'; 

	reverse(str, i); 
	str[i] = '\0'; 
	return i; 
} 

// Converts a floating point number to string. 
void ftoa(float n, char *res, int afterpoint) 
{ 
	// Extract integer part 
	int ipart = (int)n; 

	// Extract floating part 
	float fpart = n - (float)ipart; 

	// convert integer part to string 
	int i = intToStr(ipart, res, 0); 

	// check for display option after point 
	if (afterpoint != 0) 
	{ 
		res[i] = '.'; // add dot 

		// Get the value of fraction part upto given no. 
		// of points after dot. The third parameter is needed 
		// to handle cases like 233.007 
		fpart = fpart * pow(10, afterpoint); 

		intToStr((int)fpart, res + i + 1, afterpoint); 
	} 
} 

// driver program to test above funtion 
int main() 
{ 
	char res[20]; 
	float n = 233.007; 
	ftoa(n, res, 4); 
	printf("\n\"%s\"\n", res); 
	return 0; 
} 

 

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
有两种可能的解释,分别是将double数组中的每个元素换为char类型,或者将整个double数组换为一个表示其值的字符串。 第一种情况,将double数组中的每个元素换为char类型,可以使用以下代码: ```c++ #include <iostream> int main() { double arr[] = {1.0, 2.5, 3.8}; char str[10]; for (int i = 0; i < 3; i++) { str[i] = arr[i] + '0'; //将double换为char值 } str[3] = '\0'; //在字符串末尾添加空字符,表示字符串的结束 std::cout << str << std::endl; //输出字符串 return 0; } ``` 这里假设double数组中的每个元素都小于10,因此可以将其换为char类型的数字。如果double数组中的元素比较大,可以考虑使用sprintf函数将其换为字符串,如下所示: ```c++ #include <cstdio> #include <iostream> int main() { double arr[] = {10.0, 25.6, 38.9}; char str[20]; for (int i = 0; i < 3; i++) { sprintf(str + i * 6, "%.2lf", arr[i]); //将double值格式化为字符串 } std::cout << str << std::endl; //输出字符串 return 0; } ``` 这里使用sprintf函数将double值格式化为字符串,并将其存储在str数组中。由于每个double值都需要6个字符的空间(包括小数点和负号),因此可以将数组的起始位置设置为i * 6。 第二种情况,将整个double数组换为一个表示其值的字符串,可以使用stringstream类,如下所示: ```c++ #include <iostream> #include <sstream> int main() { double arr[] = {1.0, 2.5, 3.8}; std::stringstream ss; for (int i = 0; i < 3; i++) { ss << arr[i] << " "; //将double值输出到stringstream中 } std::string str = ss.str(); //将stringstream换为字符串 std::cout << str << std::endl; //输出字符串 return 0; } ``` 这里使用stringstream类将double值输出到一个字符串流中,并在每个值之间添加一个空格。最后,使用stringstream的str函数将其换为一个字符串,可以方便地进行输出和处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值