Rust program to convert a float number into an integer number

Rust | Convert float to int: Given a float number, we have to convert it into an integer number.
Submitted by Nidhi, on October 07, 2021

Problem Solution:

In this program, we will create a float variable then we will assign the value of the float variable by converting it into an integer variable using the "as" keyword.

Program/Source Code:

The source code to convert a float number into an integer number is given below. The given program is compiled and executed successfully.

// Rust program to convert float number 
// into an integer number

fn main() 
{
    let mut floatVar:f32 = 5.64;
    let mut intVar:i32 = 0;
    
    intVar = floatVar as i32;
    println!("Number is : {}",intVar);
}

Output:

Number is : 5

Explanation:

Here, we created a variable floatVar of the f32 type. Then we assigned the value of the floatVar variable into the intVar variable using the "as" keyword. After that, we printed the result.

Rust Basic Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.