Home >Backend Development >C++ >C++ program to convert int variable to double

C++ program to convert int variable to double

WBOY
WBOYforward
2023-08-29 13:17:171795browse

C++ program to convert int variable to double

In C, variables of type Int are used to store positive or negative integer values, but this type cannot contain decimal values. For this, there are float and double values. The Double data type is specifically designed to retain decimal values ​​to seven decimal places. Conversions between integer and double variables can be handled automatically by the compiler, called "implicit" conversions, or they can be explicitly triggered to the compiler by the programmer. We will discuss different conversion methods in the following chapters.

Implicit conversion

Implicit type conversion is automatically completed by the compiler. To achieve this, we need two variables; one of type integer and the other of type floating point. Then we just assign the integer value or variable to the floating point variable and everything else will be taken care of by the compiler.

algorithm

  • Takes an integer value as input.
  • Assign the value to a double variable.
  • Display output.

grammar

int input = <integer value>;
double output = input;

Example

#include <iostream>
using namespace std;

double solve(int value) {
   double opVal = value;
   return opVal;
}

int main()
{
   int ip = 25;
   double op = solve(ip);
   cout<< "The input value is: " << ip << endl;
   cout<< "The output value is: " << op << endl;
   return 0;
}

Output

The input value is: 25
The output value is: 25

As we can see, the conversion process is very simple. We don't need to do anything special, just assign the input variable to the output variable.

Explicit conversion

Explicit conversion occurs when the programmer explicitly instructs the compiler to convert one data type to another. This can be achieved in two ways, one is to specify the data type during assignment, the other is to use static_cast. We describe the first method first.

algorithm

  • Take integer value as input;
  • Use explicit conversion to boolean to assign the value to a double variable.
  • Display output.

Mention the data type during assignment

This can also be done in two different ways. One is a C-style version and the other is a functional-style conversion.

The Chinese translation of

C-Styled version

is:

C-Styled version

We mentioned the result data type before the source variable or the value contained in parentheses.

grammar

int input = <integer value>;
double output = (double) input;

Example

#include <iostream>
using namespace std;
double solve(int value) {
   double opVal = (double) value;
   return opVal;
}

int main()
{
   int ip = 35;
   double op = solve(ip);
   cout<< "The input value is: " << ip << endl;
   cout<< "The output value is: " << op << endl;
   return 0;
}

Output

The input value is: 35
The output value is: 35

Functional style type conversion

We mentioned the result data type and enclose the source value in parentheses when passing parameters to the function.

grammar

int input = <integer value>;
double output = double(input);

Example

#include <iostream>
using namespace std;
double solve(int value) {
   double opVal = double(value);
   return opVal;
}

int main()
{
   int ip = 45;
   double op = solve(ip);
   cout<< "The input value is: " << ip << endl;
   cout<< "The output value is: " << op << endl;
   return 0;
}

Output

The input value is: 45
The output value is: 45

Use static_cast

grammar

int input = <integer value>;
double output = static_cast<double>(input);

Example

#include <iostream>
using namespace std;

double solve(int value) {
   double opVal = static_cast<double>(value);
   return opVal;
}

int main()
{
   int ip = 55;
   double op = solve(ip);
   cout<< "The input value is: " << ip << endl;
   cout<< "The output value is: " << op << endl;
   return 0;
}

Output

The input value is: 55
The output value is: 55

From the last three examples, we can see that the explicit conversion process is almost similar whether using static_cast, C-style conversion, or functional-style conversion. In all three cases we have to mention the result data type before assignment.

in conclusion

Covers several methods of converting integers to double values. Programmers must determine which conversion method is best for a specific situation, because different conversion scenarios require different conversion methods. However, because implicit conversions occur automatically, programmers don't have to worry about enforcing complex strategies.

The above is the detailed content of C++ program to convert int variable to double. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete