我尝试了所有其他方法来执行 typecaste int 浮动,但我无法做到这一点......请帮助
#include <iostream>
using namespace std;
int main()
{
string s="50p";
int a=stoi(s);
float b=(float)a;
cout<<b;
return 0;
}
Expected output:- 50.00
演员阵容没有任何问题。
如果你想强制输出包含 2 位小数,你必须添加格式化语句(需要
#include <iomanip>
):
#include <iostream>
#include <string>
#include <iomanip>
int main()
{
std::string s = "50p";
int a = std::stoi(s);
float b = (float)a;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << b;
}
输出:
50.00