Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Type Casting in Programming


What is Type Casting?

Type casting (or type conversion) is the process of changing a value from one data type into another.

This is useful when you want to use a value in a calculation, but the value is stored as another type, like a text string.

For example, if we have the string "5" and the number 10, we cannot directly add them together. First we need to convert the string into a number:


a = "5"
b = 10
c = int(a) + b
print(c)
const a = "5";
const b = 10;
const c = parseInt(a) + b;
console.log(c);
String a = "5";
int b = 10;
int c = Integer.parseInt(a) + b;
System.out.println(c);
string a = "5";
int b = 10;
int c = stoi(a) + b;
cout << c;
Try it Yourself »

Implicit vs Explicit Casting

There are two types of type casting:

  • Implicit casting (also called type promotion) - done automatically by the programming language.
  • Explicit casting - when the programmer converts the type manually.

For example, in many languages an integer can be automatically promoted to a float if needed:


x = 5      # int
y = 2.0    # float
z = x + y  # int automatically becomes float
print(z)
let x = 5;      // number
let y = 2.0;    // number
let z = x + y;  // no problem, result is a number
console.log(z);
int x = 5;
double y = 2.0;
double z = x + y;  // int promoted to double
System.out.println(z);
int x = 5;
double y = 2.0;
double z = x + y;  // int promoted to double
cout << z;

Explicit casting is when you convert the type yourself:

Different languages do this in different ways:

  • Python and JavaScript use functions like float() or Number().
  • Java and C++ often use parentheses with the type in front of the value, like (double) x.

x = 5
y = float(x)  # explicit casting with function
print(y)
let x = 5;
let y = Number(x);   // explicit casting with function
console.log(y);
int x = 5;
double y = (double) x;   // explicit casting with (type)
System.out.println(y);
int x = 5;
double y = (double) x;   // explicit casting with (type)
cout << y;

Note: Type casting rules can vary between programming languages. Always be careful when converting between types, because some conversions may lose information (for example, converting a float like 3.9 into an integer becomes 3).


Real-Life Example

Type casting is often needed when doing calculations. For example, when dividing two integers, you might want a decimal result instead of a whole number.

Here is a program that calculates the percentage of a user's score compared to the maximum score in a game. We cast the value to a decimal type to get an accurate result:


max_score = 500
user_score = 423

# Convert user_score to float to get a decimal result
percentage = float(user_score) / max_score * 100.0

print("User's percentage is", round(percentage, 2))
const maxScore = 500;
const userScore = 423;

// Convert userScore to number (it already is, but ensures float division)
const percentage = Number(userScore) / maxScore * 100.0;

console.log("User's percentage is " + percentage.toFixed(2));
int maxScore = 500;
int userScore = 423;

// Cast to double to make sure the division is accurate
double percentage = (double) userScore / maxScore * 100.0;

System.out.printf("User's percentage is %.2f", percentage);
int maxScore = 500;
int userScore = 423;

// Cast to float to make sure the division is accurate
float percentage = (float) userScore / maxScore * 100.0;

printf("User's percentage is %.2f", percentage);
Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.