Hello, I need your help !


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; 
yes sir,
It print a integer not a double
ot print : 8
A quick example of how to display the decimals:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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
Last edited on
Topic archived. No new replies allowed.