How can I convert a string from hh:mm:ss to hours ...
# android
m
How can I convert a string from hhmmss to hours in double? string “002000” expected result 0.333333333333333 string “010000" expected result 1
K 1
😶 2
It’s Kotlin
f
Copy code
val result = "01:02:03".split(":").let {
        val hours = it[0]
        val minutes = it[1]
        val seconds = it[2]
        hours.toFloat() + minutes.toFloat() / 60f + seconds.toFloat() / 3600f
    }
👋 1
m
and viceversa maybe @Francesc btw you know how to go from double to string with this format “hhmmss”
f
that's easier,
hours.toString().padStart(2, '0') + ":" + minutes.toString().padStart(2, '0') + ":" + seconds.toString().padStart(2, '0')
that's for integers, just round your double to an integer
that leaves the part from double to 3 sections
m
for example if I have the value 1 I would like to convert it to the string “010000”. for example if I have the value 0.333333333333333 I would like to convert it to the string “002000".
f
I'll help you out on this one, but you need to bang your head and solve these on your own
Copy code
val value = 2.3416667
    val hours = value.toInt()
    val minutes = ((value - hours) * 60.0).toInt()
    val seconds = ((value - hours - minutes / 60.0) * 3600.0).toInt()
    println(hours.toString().padStart(2, '0') + ":" + minutes.toString().padStart(2, '0') + ":" + seconds.toString().padStart(2, '0'))
👋 1
449 Views