Open In App

Convert int to float

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Converting an integer to a floating-point number involves changing the data type of the variable from int to float, allowing decimal values. This conversion is useful when performing arithmetic operations that result in fractional values or when working with numerical data that requires precision beyond whole numbers. In this article, we will explore different ways to convert int data types to float data types.

Convert int to float Using Implicit Type Conversion:

In Implicit Type Conversion, the compiler automatically converts data types as needed during expressions

C++
#include <iostream>
using namespace std;

int main() {
    // Declaring and initializing variables
    int integerValue = 10;
    double doubleValue = 5.5;

    // integer_value is implicitly promoted to double for the addition
    double result = integerValue + doubleValue;

    // Output the result
    cout << result << endl;  // Output will be 15.5

    return 0;
}
C
#include <stdio.h>

int main() {
    // Declaring and initializing variables
    int integerValue = 10;
    double doubleValue = 5.5;

    // integer_value is implicitly promoted to double for the addition
    double result = integerValue + doubleValue;

    // Output the result
    printf("%f\n", result);  // Output will be 15.5

    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        // Declaring and initializing variables
        int integerValue = 10;
        double doubleValue = 5.5;

        // integer_value is implicitly promoted to double for the addition
        double result = integerValue + doubleValue;

        // Output the result
        System.out.println(result);  // Output will be 15.5
    }
}
C#
using System;

class MainClass {
    public static void Main (string[] args) {
        // Declaring and initializing variables
        int integerValue = 10;
        double doubleValue = 5.5;

        // integer_value is implicitly promoted to double for the addition
        double result = integerValue + doubleValue;

        // Output the result
        Console.WriteLine(result);  // Output will be 15.5
    }
}
JavaScript
// Declaring and initializing variables
let integerValue = 10;
let doubleValue = 5.5;

// integer_value is implicitly converted to double for the addition
let result = integerValue + doubleValue;

// Output the result
console.log(result);  // Output will be 15.5
Python3
# Using Implicit Type Conversion
integer_value = 10
float_value = 5.5

result = integer_value + float_value  # Implicitly converts integer_value to float
print(result)  # Output will be 15.5

Output
15.5


Convert int to float Using Explicit Type Conversion:

In Explicit Type Conversion,we manually convert one data type to another using built-in functions such as int(), float(), str(), etc.

C++
#include <iostream>
using namespace std;

int main() {
    int integerValue = 10;

    // Converting integer to float explicitly
    float floatValue = static_cast<float>(integerValue);
   cout << floatValue <<endl; // Output will be 10.0

    return 0;
}
C
#include <stdio.h>

int main() {
    int integerValue = 10;

    // Converting integer to float explicitly
    float floatValue = (float)integerValue;
    printf("%f\n", floatValue); // Output will be 10.0

    return 0;
}
Java
public class Main {
    public static void main(String[] args) {
        int integerValue = 10;

        // Converting integer to float explicitly
        float floatValue = (float) integerValue;
        System.out.println(floatValue); // Output will be 10.0
    }
}
C#
using System;

class MainClass {
    public static void Main (string[] args) {
        int integerValue = 10;

        // Converting integer to float explicitly
        float floatValue = (float)integerValue;
        Console.WriteLine(floatValue); // Output will be 10.0
    }
}
JavaScript
let integerValue = 10;

// Converting integer to float explicitly
let floatValue = parseFloat(integerValue);
console.log(floatValue); // Output will be 10.0
Python3
# Using Explicit Type Conversion
integer_value = 10

# Converting integer to float explicitly
float_value = float(integer_value)
print(float_value)  # Output will be 10.0

Output
10


Convert int to float in C:

Here are the implementation of int to float in C language:

C
#include <stdio.h>

int main() {
    int intValue = 10;
    float floatValue = (float)intValue; // Using type casting
    printf("Integer value: %d\n", intValue);
    printf("Float value: %f\n", floatValue);
    return 0;
}

Output
Integer value: 10
Float value: 10.000000


Convert int to float in C++:

Here are the implementation of int to float in C++ language:

C++
#include <iostream>
using namespace std;

int main() {
    int intValue = 10;
    float floatValue = static_cast<float>(intValue); // Using static_cast<>
    cout << "Integer value: " << intValue << endl;
    cout << "Float value: " << floatValue << endl;
    return 0;
}

Output
Integer value: 10
Float value: 10


Convert int to float in Java:

Here are the implementation of int to float in java language:

Java
public class IntToFloatExample {
    public static void main(String[] args) {
        int intValue = 10;
        float floatValue = (float)intValue; // Using type casting
        System.out.println("Integer value: " + intValue);
        System.out.println("Float value: " + floatValue);
    }
}

Output
Integer value: 10
Float value: 10.0


Convert int to float Python:

Here are the implementation of int to float in python language:

Python
intValue = 10
floatValue = float(intValue) # Using float() function
print("Integer value:", intValue)
print("Float value:", floatValue)

Output
('Integer value:', 10)
('Float value:', 10.0)


Convert int to float in C#:

Here are the implementation of int to float in C# language:

C#
using System;

class Program {
    static void Main(string[] args) {
        int intValue = 10;
        float floatValue = (float)intValue; // Using type casting
        Console.WriteLine("Integer value: " + intValue);
        Console.WriteLine("Float value: " + floatValue);
    }
}

Output
Integer value: 10
Float value: 10


Convert int to float in Javascript:

Here are the implementation of int to float in javascript language:

JavaScript
let intValue = 10;
let floatValue = parseFloat(intValue); // Using parseFloat() function
console.log("Integer value:", intValue);
console.log("Float value:", floatValue);

Output
Integer value: 10
Float value: 10


Conclusion:

Converting an integer to a float involves explicitly casting the integer value to a float type. This conversion allows the integer value to be treated as a floating-point number with decimal precision.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads