Type Casting between Basic Data Types
Numeric type casting is a common operation, involving the conversion between integers and floating-point numbers. String to numeric and character to numeric casting also play essential roles in handling diverse data formats.
Casting between basic data types is a common operation in programming, allowing developers to convert values from one data type to another. This process is essential when working with diverse types of data, enabling seamless integration and manipulation. In this section, we’ll explore how casting is performed between basic data types, including numeric types, strings, and characters.
1. Numeric Type Casting:
Integer to Float: Converting an integer to a floating-point number.
int_num = 5 float_num = float(int_num) # Casting integer to float
Float to Integer: Truncating the decimal part of a floating-point number to obtain an integer.
float_num = 7.8int_num = int(float_num) # Casting float to integer (truncation)
Numeric String to Integer or Float: Converting a string containing numeric characters to an integer or a float.
str_num = “10”int_num = int(str_num) # Casting string to integerfloat_num = float(str_num) # Casting string to float
2. String to Numeric Type Casting:
String to Integer: Converting a string representing an integer to an actual integer.
str_num = “42”int_num = int(str_num) # Casting string to integer
String to Float: Converting a string representing a floating-point number to an actual float.
str_float = “3.14”float_num = float(str_float) # Casting string to float
3. Character to Numeric Type Casting:
Character to Integer: Converting a character to its corresponding ASCII value.
char c = ‘A’;int ascii_value = (int)c; // Casting character to integer (ASCII value)
Here’s a simple program that demonstrates casting between basic data types:
#include <iostream>
#include <string>
using namespace std;
int main() {
// Example 1: Integer to Float
int integer_value = 10;
float float_value = static_cast<float>(integer_value);
cout << "Integer to Float: " << integer_value << " -> " << float_value << endl;
// Example 2: Float to Integer
float float_value_2 = 15.5;
int integer_value_2 = static_cast<int>(float_value_2);
cout << "Float to Integer: " << float_value_2 << " -> " << integer_value_2 << endl;
// Example 3: Integer to String
int integer_value_3 = 42;
string string_value = to_string(integer_value_3);
cout << "Integer to String: " << integer_value_3 << " -> '" << string_value << "'" << endl;
// Example 4: String to Integer
string string_value_2 = "123";
int integer_value_4 = stoi(string_value_2);
cout << "String to Integer: '" << string_value_2 << "' -> " << integer_value_4 << endl;
// Example 5: String to Float
string string_value_3 = "3.14";
float float_value_3 = stof(string_value_3);
cout << "String to Float: '" << string_value_3 << "' -> " << float_value_3 << endl;
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
// Example 1: Integer to Float
int integer_value = 10;
float float_value = (float) integer_value;
System.out.println("Integer to Float: " + integer_value + " -> " + float_value);
// Example 2: Float to Integer
float float_value_2 = 15.5f;
int integer_value_2 = (int) float_value_2;
System.out.println("Float to Integer: " + float_value_2 + " -> " + integer_value_2);
// Example 3: Integer to String
int integer_value_3 = 42;
String string_value = Integer.toString(integer_value_3);
System.out.println("Integer to String: " + integer_value_3 + " -> '" + string_value + "'");
// Example 4: String to Integer
String string_value_2 = "123";
int integer_value_4 = Integer.parseInt(string_value_2);
System.out.println("String to Integer: '" + string_value_2 + "' -> " + integer_value_4);
// Example 5: String to Float
String string_value_3 = "3.14";
float float_value_3 = Float.parseFloat(string_value_3);
System.out.println("String to Float: '" + string_value_3 + "' -> " + float_value_3);
}
}
# Casting between Basic Data Types
# Example 1: Integer to Float
integer_value = 10
float_value = float(integer_value)
print(f"Integer to Float: {integer_value} -> {float_value}")
# Example 2: Float to Integer
float_value = 15.5
integer_value = int(float_value)
print(f"Float to Integer: {float_value} -> {integer_value}")
# Example 3: Integer to String
integer_value = 42
string_value = str(integer_value)
print(f"Integer to String: {integer_value} -> '{string_value}'")
# Example 4: String to Integer
string_value = "123"
integer_value = int(string_value)
print(f"String to Integer: '{string_value}' -> {integer_value}")
# Example 5: String to Float
string_value = "3.14"
float_value = float(string_value)
print(f"String to Float: '{string_value}' -> {float_value}")
// Example 1: Integer to Float
let integer_value = 10;
let float_value = parseFloat(integer_value);
console.log("Integer to Float:", integer_value, "->", float_value);
// Example 2: Float to Integer
let float_value_2 = 15.5;
let integer_value_2 = parseInt(float_value_2);
console.log("Float to Integer:", float_value_2, "->", integer_value_2);
// Example 3: Integer to String
let integer_value_3 = 42;
let string_value = integer_value_3.toString();
console.log("Integer to String:", integer_value_3, "->", "'" + string_value + "'");
// Example 4: String to Integer
let string_value_2 = "123";
let integer_value_4 = parseInt(string_value_2);
console.log("String to Integer:", "'" + string_value_2 + "'", "->", integer_value_4);
// Example 5: String to Float
let string_value_3 = "3.14";
let float_value_3 = parseFloat(string_value_3);
console.log("String to Float:", "'" + string_value_3 + "'", "->", float_value_3);
Output
Integer to Float: 10 -> 10 Float to Integer: 15.5 -> 15 Integer to String: 42 -> '42' String to Integer: '123' -> 123 String to Float: '3.14' -> 3.14
Type Casting in Programming
In programming, variables hold data of specific types, such as integers, floating-point numbers, strings, and more. These data types determine how the computer interprets and manipulates the information. Type casting becomes necessary when you want to perform operations or assignments involving different data types. In this blog, we will explore type casting, its importance, and the various methods used in different programming languages.
Table of Content
- What is Type Casting
- Types of Type Casting
- Implicit Type Casting
- Explicit Type Casting
- Difference between Implicit and Explicit Type Casting
- Type Compatibility and Safety in Programming
- Type Casting between Basic Data Types
- Challenges and Best Practices of Type Casting in Programming
Contact Us