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
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
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;
}
OutputInteger 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;
}
OutputInteger 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);
}
}
OutputInteger 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);
}
}
OutputInteger 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);
OutputInteger 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.
Similar Reads
How to convert String to Float in PHP ?
Converting a string to a float in PHP is a common requirement when handling numeric data stored in string format. This conversion allows the numeric string to be used in calculations or comparisons, ensuring accurate manipulation of data within the program.There are many methods to convert a string
3 min read
How to convert Float to Int in Python?
In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.For example:Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.4
5 min read
Convert hex string to float in Python
Converting a hex string to a float in Python involves a few steps since Python does not have a direct method to convert a hexadecimal string representing a float directly to a float. Typically, a hexadecimal string is first converted to its binary representation, and then this binary representation
3 min read
Convert Decimal to Fraction
Converting a decimal to a fraction is the process of changing a number in decimal form into a fraction, which represents the same value in terms of parts of a whole number. Converting decimals into fractions is a key skill that helps clarify values and is useful in various practical situations.Decim
6 min read
Cannot Convert String To Float in Python
Python, a versatile and powerful programming language, is widely used for data manipulation and analysis. However, developers often encounter challenges, one of which is the "Cannot Convert String To Float" error. This error occurs when attempting to convert a string to a float, but the string's con
3 min read
SQL Query to Convert FLOAT to NVARCHAR
Here we will see, how to convert FLOAT data to NVARCHAR data in an MS SQL Server's database table using the CAST(), CONVERT(), and FORMAT() functions. We will be creating a person table in a database called "geeks". Creating the Database:CREATE DATABASE geeks;Using the Database:USE geeks;Table Defin
2 min read
Convert Fractions to Decimals
Converting fractions to decimals is a process of converting a number represented in the form of p/q where q\neq 0 to decimal form. There are two main methods to convert Fractions to decimals are Long division method and the Denominator Adjustment method (for powers of 10). Example : 1.25 in decimal
6 min read
Convert String with Comma To Float in Python
When working with data in Python, it's not uncommon to encounter numeric values formatted with a mix of commas and dots as separators. Converting such strings to float is a common task, and Python offers several simple methods to achieve this. In this article, we will explore five generally used met
3 min read
How to Convert string to float type in Golang?
ParseFloat function is an in-build function in the strconv library which converts the string type into a floating-point number with the precision specified by bit size. Example: In this example the same string -2.514 is being converted into float data type and then their sum is being printed. Once i
1 min read
How to convert string into float in JavaScript?
In this article, we will convert a string into a float in Javascript. We can convert a string into a float in JavaScript by using some methods which are described below: Methods to Concert String into Float:Table of Content Method 1: By using Type Conversion of JavaScriptMethod 2: By using parseFloa
6 min read