Int to Float in Rust 1.55

Find this useful? Support us: Star on GitHub 6
Category: Number | Language: Rust 1.55

To convert an integer to a float in Rust 1.55, you can use the as keyword with the appropriate f suffix for the desired floating-point size. Here are some examples:

// Convert an i32 to a f32
let int_num: i32 = 7;
let float_num: f32 = int_num as f32;
println!("The float value is: {}", float_num); // Output: The float value is: 7.0

// Convert an i64 to a f64
let int_num: i64 = 123456789;
let float_num: f64 = int_num as f64;
println!("The float value is: {}", float_num); // Output: The float value is: 123456789.0

In the examples above, we use the as keyword to convert int_num to a float, specifying the f suffix for the desired floating-point size. Finally, we print the converted float value using println!.