将 double 值转换为 char 变量时,stringstream 如何工作
How does stringstream work when converting a double value into a char variable
我看到post 在这里询问如何将双精度变量值转换为字符数组。有人说只使用 stringstream 但没有解释它为什么有效。我尝试使用谷歌搜索,但找不到任何关于具体如何转换它的文档。我想知道是否有人可以向我解释它是如何工作的。这是我编写的将双精度变量值转换为字符数组的代码。
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
double a = 12.99;
char b[100];
stringstream ss;
ss << a;
ss >> b;
cout << b; // it outputs 12.99
return 0;
}
当您执行 ss << a;
时,您将在 stringstream
中插入双精度数(假设它在 string
中保存值),因此当您 运行 ss >> b;
它只是按字符复制 char[]
中的 string
。
现在唯一的一点是将double
转换为string
,可以通过简单的算法实现:
std::string converter(double value){
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
bool is_negative = value < 0;
std::string integer_to_string;
value = is_negative ? value * -1 : value; // make the number positive
double fract = value - static_cast<unsigned int>(value); // fractionary part of the number
unsigned int integer = static_cast<int>(value); // integer part of the number
do{
unsigned int current = integer % 10; // current digit
integer_to_string = std::string(1, digits[current]) + integer_to_string; // append the current digit at the beginning
integer = integer / 10; // delete the current digit
} while(integer > 0); // do over and over again until there are digits
integer_to_string = (is_negative ? "-" : "") + integer_to_string; // put the - in case of negative
std::string fract_to_string;
if(fract > 0) {
fract_to_string = ".";
do {
unsigned int current = static_cast<int>(fract * 10); // current digit
fract_to_string = fract_to_string + std::string(1, digits[current]); // append the current digit at the beginning
fract = (fract * 10) - current; // delete the current digit
} while (fract > 0);
}
return integer_to_string + fract_to_string;
}
请记住,这是一个非常基本的转换,由于operator-
在浮点运算中的不稳定,会出现很多错误,所以不稳定很多,但这只是一个例子
注意:这绝对是为了避免在遗留(实际上不仅仅是遗留)代码中使用,它只是作为一个例子,而不是你应该使用 std::to_string()
来执行它会更快并且没有任何类型错误(检查 this)
我看到post 在这里询问如何将双精度变量值转换为字符数组。有人说只使用 stringstream 但没有解释它为什么有效。我尝试使用谷歌搜索,但找不到任何关于具体如何转换它的文档。我想知道是否有人可以向我解释它是如何工作的。这是我编写的将双精度变量值转换为字符数组的代码。
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
double a = 12.99;
char b[100];
stringstream ss;
ss << a;
ss >> b;
cout << b; // it outputs 12.99
return 0;
}
当您执行 ss << a;
时,您将在 stringstream
中插入双精度数(假设它在 string
中保存值),因此当您 运行 ss >> b;
它只是按字符复制 char[]
中的 string
。
现在唯一的一点是将double
转换为string
,可以通过简单的算法实现:
std::string converter(double value){
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
bool is_negative = value < 0;
std::string integer_to_string;
value = is_negative ? value * -1 : value; // make the number positive
double fract = value - static_cast<unsigned int>(value); // fractionary part of the number
unsigned int integer = static_cast<int>(value); // integer part of the number
do{
unsigned int current = integer % 10; // current digit
integer_to_string = std::string(1, digits[current]) + integer_to_string; // append the current digit at the beginning
integer = integer / 10; // delete the current digit
} while(integer > 0); // do over and over again until there are digits
integer_to_string = (is_negative ? "-" : "") + integer_to_string; // put the - in case of negative
std::string fract_to_string;
if(fract > 0) {
fract_to_string = ".";
do {
unsigned int current = static_cast<int>(fract * 10); // current digit
fract_to_string = fract_to_string + std::string(1, digits[current]); // append the current digit at the beginning
fract = (fract * 10) - current; // delete the current digit
} while (fract > 0);
}
return integer_to_string + fract_to_string;
}
请记住,这是一个非常基本的转换,由于operator-
在浮点运算中的不稳定,会出现很多错误,所以不稳定很多,但这只是一个例子
注意:这绝对是为了避免在遗留(实际上不仅仅是遗留)代码中使用,它只是作为一个例子,而不是你应该使用 std::to_string()
来执行它会更快并且没有任何类型错误(检查 this)