首页 > 解决方案 > Julia函数将秒转换为小时、分钟、秒的问题

问题描述

我有这个代码让 Julia 将秒转换为小时、分钟和秒,但是当我运行它时,我只是得到(0, 0, 0)输出。有人可以告诉我这有什么问题吗?

function convert_from_seconds(sec::Int64)
    hours = 0
    minutes = 0
    seconds = 0

    time = (hours, minutes, seconds)

    if sec < 60
        seconds = sec
    elseif sec < 3600
        minutes = floor(sec / 60)
        seconds = sec % 60
    elseif sec < 216000
        hours = floor(sec / 3600)
        minutes = floor(hours % 3600)
        seconds = minutes % 60
    end
    return time
end

标签: julia

解决方案


这是您可能要考虑的另一种方法:

function convert_from_seconds(sec::Int)
    x, seconds = divrem(sec, 60)
    hours, minutes = divrem(x, 60)
    hours, minutes, seconds
end

推荐阅读