2016-10-12 22 views

回答

4

您的代碼實際上是不正確的:對於90,它返回"2:30"

您可以使用div做整數除法,忽略小數部分。至於其他的,我會使用字符串插值來縮短代碼:

defmodule Main do 
    def formatted_length(length) do 
    "#{div(length, 60)}:#{formatted_seconds(rem(length, 60))}" 
    end 

    defp formatted_seconds(s) when s < 10, do: "0#{s}" 
    defp formatted_seconds(s), do: "#{s}" 
end 

13 |> Main.formatted_length |> IO.puts 
123 |> Main.formatted_length |> IO.puts 
143 |> Main.formatted_length |> IO.puts 

輸出:

0:13 
2:03 
2:23 
相關問題