所以我很难弄清楚这次如何转换: 4:27.47
转换为秒的浮点值。
如果您需要更多详细信息,请随时询问。
最佳答案
#include<string>
#include<iostream>
int main(){
std::string str{ "4:27.47"};//The given string
float secs {std::stof(str) * 60+std::stof(str.substr(2))};//Your float seconds
std::cout<<secs;//Display answer
}
以下编辑使代码也适用于格式(MM:SS)
#include<string>
#include<iostream>
int main(){
size_t pos{};//To get the position of ":"
std::string str{ "4:27.47"};//The given string
float secs {std::stof(str, &pos) * 60+std::stof(str.substr(pos+1))};//Your float seconds
std::cout<<secs;//Display answer
}
关于c++ - 如何将时间字符串(M:SS)转换为float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59978640/