[英]I would appreciate help converting ints/floats to strings in c++
#include <iostream>
using namespace std;
int fiveYears;
fiveYears = 5 * 1.5;
int sevenYears;
sevenYears = 7 * 1.5;
int tenYears;
tenYears = 10 * 1.5;
int main()
{
cout << "In years the ocean's level will be higher by " << fiveYears << "millimeters\n";
cout << "In years the ocean's level will be higher by " << sevenYears << "millimeters\n";
cout << "In years the ocean's level will be higher by " << tenYears << "millimeters\n";
return 0;
}
这就是我到目前为止所拥有的。 我大约一周前才开始使用 c++,但我仍然不确定如何将浮点数和整数转换为字符串。 我的 output 应该打印带有字符串结果的语句。
你可以使用这个
#include <iostream>
using namespace std;
int fiveYears;
int sevenYears;
int tenYears;
int main()
{
fiveYears = 5 * 1.5;
sevenYears = 7 * 1.5;
tenYears = 10 * 1.5;
cout << "In years the ocean's level will be higher by " << fiveYears << " millimeters\n";
cout << "In years the ocean's level will be higher by " << sevenYears << " millimeters\n";
cout << "In years the ocean's level will be higher by " << tenYears << " millimeters\n";
return 0;
}
或这个
#include <iostream>
using namespace std;
int fiveYears = 5 * 1.5;
int sevenYears = 7 * 1.5;
int tenYears = 10 * 1.5;
int main()
{
cout << "In years the ocean's level will be higher by " << fiveYears << " millimeters\n";
cout << "In years the ocean's level will be higher by " << sevenYears << " millimeters\n";
cout << "In years the ocean's level will be higher by " << tenYears << " millimeters\n";
return 0;
}
如果你真的想使用全局变量来解决这个问题。
您可以使用std::to_string将数值转换为 std::string。
例子
#include <iostream>
int main()
{
int v1 = 61;
long v2 = 62L;
long long v3 = 63LL;
unsigned int v4 = 64;
unsigned long v5 = 65UL;
unsigned long long v6 = 66ULL;
float v7 = 67.0f;
double v8 = 68.0;
long double v9 = 69.0L;
std::string v1Str = std::to_string(v1);
std::string v2Str = std::to_string(v2);
std::string v3Str = std::to_string(v3);
std::string v4Str = std::to_string(v4);
std::string v5Str = std::to_string(v5);
std::string v6Str = std::to_string(v6);
std::string v7Str = std::to_string(v7);
std::string v8Str = std::to_string(v8);
std::string v9Str = std::to_string(v9);
std::cout << v1Str << std::endl;
std::cout << v2Str << std::endl;
std::cout << v3Str << std::endl;
std::cout << v4Str << std::endl;
std::cout << v5Str << std::endl;
std::cout << v6Str << std::endl;
std::cout << v7Str << std::endl;
std::cout << v8Str << std::endl;
std::cout << v9Str << std::endl;
int fiveYears = 5;
int sevenYears = 7;
int tenYears = 10;
float ratio = 1.5;
std::cout << u8"In " + std::to_string(fiveYears) + u8" years the ocean\'s level will be higher by " + std::to_string(fiveYears * ratio) + u8" millimeters" << std::endl;
std::cout << u8"In " + std::to_string(sevenYears) + u8" years the ocean\'s level will be higher by " + std::to_string(sevenYears * ratio) + u8" millimeters" << std::endl;
std::cout << u8"In " + std::to_string(tenYears) + u8" years the ocean\'s level will be higher by " + std::to_string(tenYears * ratio) + u8" millimeters" << std::endl;
}
Output
61
62
63
64
65
66
67.000000
68.000000
69.000000
In 5 years the ocean's level will be higher by 7.5 millimeters
In 7 years the ocean's level will be higher by 10.5 millimeters
In 10 years the ocean's level will be higher by 15 millimeters
在https://repl.it/@JomaCorpFX/IntFloatsToString#main.cpp中检查/运行此示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.