Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何将MM:SS:splits转换为MM:SSSS?_Ruby On Rails_Ruby_Time - Fatal编程技术网

Ruby on rails 如何将MM:SS:splits转换为MM:SSSS?

Ruby on rails 如何将MM:SS:splits转换为MM:SSSS?,ruby-on-rails,ruby,time,Ruby On Rails,Ruby,Time,我有一个音频标签,看起来像这样:“00:14:164”,即“MM:SS:splits” 我希望格式是数字,并且只以秒为单位 例如: “00:14:164”将是14.164 及 “01:59:582”将是119.582 这样做的最佳方式是什么 def tag_to_f(tag) a = tag.split(':').map(&:to_i) "#{a[0] * 60 + a[1]}.#{a[2]}".to_f end tag_to_f "01:59:582" 或猴子补丁Strin

我有一个音频标签,看起来像这样:“
00:14:164
”,即“
MM:SS:splits

我希望格式是数字,并且只以秒为单位

例如:

00:14:164
”将是
14.164

01:59:582
”将是
119.582

这样做的最佳方式是什么

def tag_to_f(tag)
  a = tag.split(':').map(&:to_i)
  "#{a[0] * 60 + a[1]}.#{a[2]}".to_f
end

tag_to_f "01:59:582"
或猴子补丁
String

class String
  def to_f_with_audio_tag
    if a = match(/^(\d+):(\d+):(\d+)$/).to_a[1..3].map(&:to_i) rescue nil
      "#{a[0] * 60 + a[1]}.#{a[2]}".to_f_without_audio_tag
    else
      to_f_without_audio_tag
    end
  end

  alias_method_chain :to_f, :audio_tag
end

"01:59:582".to_f

您是希望在视图中以秒为单位显示该值,还是希望在数据库中以秒为单位存储该值?