【问题标题】:How to convert a string of a time (M:SS) to float如何将时间字符串(M:SS)转换为浮点数
【发布时间】:2020-01-30 03:20:00
【问题描述】:

所以这次我无法弄清楚如何转换:4:27.47

转换为秒的浮点值。

如果您需要更多详细信息,请随时询问。

【问题讨论】:

    标签: c++ string c++11


    【解决方案1】:
    #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
    }
    

    【讨论】:

    • 这仅在分钟仅包含一位数字时才有效。字符串是 M:SS,而不是 H:MM,所以你的结果是正确值的 60 倍
    • @phuclv OP 说 (M:SS)。无论如何我提供了一个补充。谢谢
    【解决方案2】:
    #include<stdio.h>
    int main()
    {
        double minute, second;
        scanf("%lf : %lf", &minute, &second);
        printf("%f\n", minute * 60 + second);
    }
    

    【讨论】:

      【解决方案3】:

      有点冗长的解决方案,但无论是否给定时间,它都有效:

      (HH:MM:SS.Milli) || (MM:SS.Milli) || (SS.Milli) || (.Milli)

      double parse_text_minutes_to_double(std::string original)
      {
          std::vector<std::string> hms;
          std::size_t pos;
          while(std::count(original.begin(), original.end(), ':') )
          {
              pos  = original.find(':');
              hms.push_back(original.substr(0, pos));
              original = original.substr(pos+1);
          }
      
          int minutes_hours{};
          double sec_and_milli = std::stof(original);
          int iteration_count{};
          while (!hms.empty())
          {
              ++iteration_count;
              int seconds_iteration = std::stoi(hms.back()) * std::pow(60, iteration_count);
              hms.pop_back();
              minutes_hours += seconds_iteration;
          }
          return minutes_hours + sec_and_milli;
      }
      
      int main()
      {
          std::string original("1:1:20.465");
          double result = parse_text_minutes_to_double(original);
      
          std::cout << result << std::endl;
      }
      

      【讨论】:

        【解决方案4】:

        可以用冒号(:)分割字符串,然后将字符串转换为float,计算总秒数如下

        #include <iostream>
        #include <cstdlib> 
        
        using namespace std;
        
        int main(){
        
            string time = "4:27.47";
            string minutes = "";
            string seconds = "";
            string temp = "";
            float m, s;
        
            for(int i=0; i<time.length(); i++){
                if(time[i]==':'){
                    minutes = temp;
                    temp = "";
                }
                else
                    temp += time[i];
                if(i==time.length()-1)
                    seconds = temp;
            }
        
            // casting string to float
            m = atof(minutes.c_str());
            s = atof(seconds.c_str());
        
            float total = m*60 + s;
            cout << total;
        
            return 0;
        
        }
        
        猜你喜欢
        • 2011-11-25
        • 2019-06-20
        • 1970-01-01
        • 2018-02-15
        • 2011-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多