Typecasting is a procedure for transforming data between different data types. This data conversion method is also termed type conversion or type coercion. Casting allows the data type to be modified rather than the data itself. C++ supports typecasting for all the data types. This article will discuss one typecasting method: the conversion from int data type to double data type. We will explore different approaches that simply convert int to double data type.
Two Categories of Conversion in C++
Implicit Type Conversion
The implicit type conversion is done spontaneously. In this conversion, the user has no input, and the compiler does the conversion entirely on its own. When there are many types of data in an expression, conversion is frequently performed. However, there is a risk of data loss, sign loss, or data overflow with this form of conversion.
Explicit Type Conversion
The Explicit type is defined by the user and is sometimes referred to as “type-casting.” Depending on the needs, the user translates or converts data of one data type to another. These types of conversions are more secure.
Example 1
The following example is the working of implicit type conversion, which converts int to double data type. The program starts from the header files. We have two header files in the header section. Then there’s the code implementation’s main function. In the main function, we have defined a variable as “IntNumber” of integer data type and initialized it with the integral value.
Likewise, we have created another variable as “DoubleNumber” of data type double, but it is not initialized. Now, we have to convert the int data type value to the double data type value. So, we have assigned the int variable “IntNumer” to a double data type variable “DoubleNumber”. This approach is the implicit type conversion. The values of int data type and double data type will be printed through the C++ cout command.
Before assigning the int value to the double variable, the compiler transforms it to double automatically. As you can see in the image, the int cannot include a decimal value, and the digits after the decimal point are truncated in this example.
Example 2
We have seen the conversion of int to double in the preceding example. Now, we will convert double to int, which is also done through implicit conversion.
In the first step, we have included iostream and std namespace files. Then, we invoked the program’s main function, where we declared an integer data type variable “IntVal”. Also, we have declared another variable of double data type as “DoubleVal” and initialized it with the double value as it contains a decimal part. For the conversion, we have assigned the double variable to the int variable. The implicit type conversion value will be printed on the console screen when executing the program.
You can see the double value and the conversion of double data in the int data as the output.
Example 3
As we can see from the previous example, converting from one data type to another is a chance of data loss. This occurs when larger-type data is transformed into smaller-type data. To overcome the data loss problem, we performed explicit type conversion. The explicit type casting is done through the C-style typecasting. As the name implies, it favors a C language form of casting. Cast notation is another term for it.
The program begins with the header section, which includes the C++ files. In the next step, the main function is created where the variable is defined as “IntNumber” of data type “int” and stores the integral value. Another variable is declared as “DoubleNumber” of data type “double”.
Then, we have used the C-style type casting method, which uses the type double notation with the int variable and assigns it to the double variable, “DoubleNumber”. These values will be printed by using the C++ cout command.
The decimal part is truncated as the int data type has no data type.
Example 4
The notation function can also move data between different data types. The following program uses function style casting to convert int to double data type.
We have the main function where we have created an int data type variable as “Integer_x” and initialized it with the numeric value “20”. The double type variable is also defined as “Double_y”. Then, we used function typecasting to convert int data to double type. We have passed the int variable “Integer_x” as the function and defined it with the data type “double”. The variable “Double y” is subsequently assigned to the function.
The outcome of the function style casting performed on int type to convert into double data type displayed on the following image:
Example 5
For type conversion of double to int, we implemented both C-style type conversion and function-style casting. This will show the results of the two ways of explicit typecasting.
In the main function block of this program, we have a double data type variable as “DoubleX” and initialized with the decimal value. Then, we created another two variables, “Integer1” and “Integer2”, and assigned a data type int. The int variable “Integer1” is used for C-style conversion, and the int variable “Integre2” is used for function style conversion. The converted values into integers from double data will be printed after performing both the conversion.
As both ways of explicit conversion do the same thing, they produce the same following results:
Conclusion
We will conclude our discussion of typecasting of int to double in C++. We have learned about the implicit and explicit conversions of int data to double and double data to int data type used in C++. However, conversions or typecasting can only be used carefully and only when necessary to avoid data loss and other problems.