How to convert hh:mm:ss to minutes in Rust

1 Answer

0 votes
fn hhmmsstominutes(hhmmss: &str) -> f64 {
    let time: Vec<i32> = hhmmss
        .split(':')
        .map(|s| s.parse::<i32>().unwrap_or(0))
        .collect();

    (time[0] * 60) as f64 + time[1] as f64 + (time[2] as f64 / 60.0)
}

fn main() {
    println!("{}", hhmmsstominutes("2:30:00"));
    println!("{}", hhmmsstominutes("2:35:30"));
    println!("{}", hhmmsstominutes("5:00:45"));
}



/*
run:

150
155.5
300.75

*/

 



answered Apr 17 by avibootz

Related questions

1 answer 31 views
1 answer 35 views
1 answer 37 views
1 answer 31 views
1 answer 33 views
...