Convert float to integer in Rust

后端 未结 3 1305
鱼传尺愫
鱼传尺愫 2021-02-03 19:02
double b = a / 100000;
b = (int) b;
b *= 100000;

How the above C code is converted to Rust? Especially the line #2 that rounds the number.

3条回答
  •  有刺的猬
    2021-02-03 19:18

    This is an example of round in Rust. You have to write numeric constants right the type they are: for example if d is f64 and you want to multiply it by 10 the right convention is to write: d * 10.0 instead of: d * 10

    and explicitly define the type of your variable to make the round function available in this cases.

    let a = 2e50;
    let mut b : f64 = a / 100000.0;
    b = b.round();
    println!("{}", b);
    

提交回复
热议问题