How to round a floating point number and remove digits after decimal point in Rust

1 Answer

0 votes
fn main() {
    let x: f32 = 4.311;
    let y: f32 = 6.500;
    let z: f32 = 9.811;

    println!("{}", x.trunc());
    println!("{}", y.trunc());
    println!("{}", z.trunc());

    let x: f32 = -4.311;
    let y: f32 = -6.500;
    let z: f32 = -9.811;

    println!("{}", x.trunc());
    println!("{}", y.trunc());
    println!("{}", z.trunc());
}


  
/*
run:

4
6
9
-4
-6
-9

*/

 



answered Sep 18, 2024 by avibootz
...