How to get the first digit after the decimal point of a float number in Rust

1 Answer

0 votes
fn first_decimal_digit(f: f32) -> u32 {
    ((f.abs() * 10.0) as i32 % 10) as u32
}

fn main() {
    let f = 983.6571_f32;
    
    println!("{}", first_decimal_digit(f)); // 6
}


/*
run:

6

*/

 



answered Oct 30, 2024 by avibootz
edited Sep 16 by avibootz

Related questions

...