I did declared 2 variables of type double, and I declared third variable which is the sum of the two doubles, and when I print the sum it didn't print a double ?
1 2 3 4
double a = 4.0;
double b = 4.0;
double sum = a + b;
cout << sum << endl;
#include <iostream>
#include <iomanip> // Need it for fixed and setprecision().
int main()
{
double a = 4.0;
double b = 4.0;
double sum = a + b;
// Run a for loop to display the number of decimals.
for (int count = 0; count < 10; count++)
{
std::cout << "Printing with " << count << " decimal...\n";
std::cout << std::fixed << std::setprecision(count) << sum << std::endl;
}
return 0;
}
Output:
Printing with 0 decimal...
8
Printing with 1 decimal...
8.0
Printing with 2 decimal...
8.00
Printing with 3 decimal...
8.000
Printing with 4 decimal...
8.0000
Printing with 5 decimal...
8.00000
Printing with 6 decimal...
8.000000
Printing with 7 decimal...
8.0000000
Printing with 8 decimal...
8.00000000
Printing with 9 decimal...
8.000000000