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

C++ program to convert double type variable to int type

PHPz
PHPzforward
2023-08-25 20:25:185632browse

C++ program to convert double type variable to int type

In C, variables of type int can only hold positive or negative integer values; they cannot hold decimal values. There are float and double values ​​available for this purpose. The double data type was created to store decimals up to seven digits after the decimal point. Conversion of an integer to a double data type can be done automatically by the compiler (called an "implicit" conversion), or it can be explicitly requested by the programmer from the compiler (called an "explicit" conversion). In the following sections, we'll cover various conversion methods.

Implicit conversion

The compiler automatically performs implicit type conversion. To achieve this, two variables are required - one of floating point type and the other of integer type. When we simply assign a floating point value or variable to an integer variable, the compiler takes care of all the other things. This conversion suffers from data loss because integer variables cannot contain decimal values ​​after the decimal point.

grammar

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

algorithm

  • Take double precision value as input;
  • Assign the value to the integer variable.
  • Display output.

Example

#include <iostream>
using namespace std;

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

int main()
{
   double ip = 25.3056;
   int 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.3056
The output value is: 25

As we can see, the conversion process is quite simple. We just assign the input variables to the output variables; no additional procedures are required. Also, you can see that the fractional part of the double value is not present in the output.

Explicit conversion

When the programmer explicitly instructs the compiler to convert one data type to another, this is called an explicit conversion or explicit type conversion. There are two ways to achieve this: one is to explicitly declare the data type during assignment, and the other is to use static_cast. We discussed the first method before.

algorithm

  • Take double precision value as input;
  • Use explicit type conversion to assign values ​​to integer variables.
  • Display output.

Mention data type during assignment

There are two different execution methods. One is a C-style version and the other is a functional-style conversion.

C style version

The result data type is specified before the source variable, enclosed in parentheses.

grammar

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

Example

#include <iostream>
using namespace std;

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

int main()
{
   double ip = 84.4439;
   int op = solve(ip);
   cout<< "The value before conversion: " << ip << endl;
   cout<< "The value after conversion: " << op << endl;
   return 0;
}

Output

The value before conversion: 84.4439
The value after conversion: 84

Functional casting

When providing parameters to a function, we declare the result data type and enclose the source value in parentheses.

grammar

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

Example

#include <iostream>
using namespace std;

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

int main()
{
   double ip = -993.6571;
   int op = solve(ip);
   cout<< "The value before conversion: " << ip << endl;
   cout<< "The value after conversion: " << op << endl;
   return 0;
}

Output

The value before conversion: -993.657
The value after conversion: -993

Use static_cast

To convert between predefined types, use static casts. Additionally, this cast, which can also be referenced explicitly, is responsible for enforcing the implicit type conversion.

grammar

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

Example

#include <iostream>
using namespace std;

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

int main()
{
   double ip = -65.2354;
   int op = solve(ip);
   cout<< "The value before conversion: " << ip << endl;
   cout<< "The value after conversion: " << op << endl;
   return 0;
}

Output

The value before conversion: -65.2354
The value after conversion: -65

in conclusion

Converting from a double to an integer data type always results in data loss because an integer variable cannot contain the fractional part of a double variable. These conversions are useful when we have to round a value to its lower bound value (the smallest integer value given a decimal value).

The above is the detailed content of C++ program to convert double type variable to int type. 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