简体   繁体   English

如何将十进制转换为时间格式? kotlin Kotlin

[英]How to convert decimal to time format? kotlin Kotlin

val duration = df.format(videoView!!.duration.toLong() / 100000.0)

duration gives the answer in decimals eg 0.45 I want to make it like 0:45.持续时间以小数形式给出答案,例如 0.45 我想让它像 0:45。

(these are in seconds, I dont want to display units. Writing this info in brackets just for reference) (这些以秒为单位,我不想显示单位。将此信息写在括号中仅供参考)

Dividing milliseconds by 100,000 doesn't really give you anything.将毫秒除以 100,000 并没有真正给你任何东西。 It is much better to divide by 1000 (not by 1000.0. Because, you will probably not need milliseconds, you can also round to closest integer value, but that's up to you):最好除以 1000(而不是除以 1000.0。因为您可能不需要毫秒,您也可以四舍五入到最接近的 integer 值,但这取决于您):

val durationInSeconds:Int = df.format(videoView...duration.toLong() / 1000)

Then you can calculate minutes by dividing duratinInSeconds by 60 (result is integer).然后您可以通过将duratinInSeconds除以 60 来计算分钟(结果是整数)。

val minutes:Int = durationInSeconds/60

Then you calculate seconds:然后计算秒数:

val seconds:Int = durationInSeconds%60

Lastly you combine minutes and seconds :最后你结合minutesseconds

val minutesAndSeconds = "%2d:%2d".format(minutes,seconds)

I would first try to get my time value either in seconds or in milliseconds and then try to format it something like this:我会首先尝试以秒或毫秒为单位获取我的时间值,然后尝试将其格式化为:

//t is given in seconds
fun formatTime(t: Int) : String{
    var hours = t / 3600
    var minutes = (t % 3600) / 60
    var seconds = t % 60

    return "$hours:$minutes:$seconds"
}
print(formatTime(12345)) //prints 3:25:45

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM