how to type cast quotient of two integers to double with c++ code example

Example: how to type cast quotient of two integers to double with c++

// Utilize floating point numbers (`float` or `double`) by typecasting 
int a = 5;
int b = 10;
double c = ((double)a) / ((double) b); 
// This ensures that a and b are treated as doubles before being divided. 
// If utilizing raw numbers, ensure to specify to the compiler that the number is a double 
// or float like so: 
double c = 5. / 10.; // The decimal (.) specifies a double 
double d = 5.f / 10.f;; // the f specifies a float

Tags:

Cpp Example