如果你使用的是C++,你也可以使用std::stof函数:
- #include <iostream>
- #include <string>
- int main() {
- std::string str = "3.14"; // 你的字符串
- // 使用std::stof进行转换
- float floatValue = std::stof(str);
- // 打印转换后的浮点型数
- std::cout << "转换后的浮点数为: " << floatValue << std::endl;
- return 0;
- }
这里使用std::stof而不是std::atof,因为C++中引入了更安全和灵活的字符串转换函数。
|