The C++ language provides numerous data types to work with like “int”, “float”, “char”, “double”, “long double”, etc. The “double” data type is utilized for the numbers that contain decimal points up to “15” or for the exponential values. It can carry twice as much information and data as a float which is called a double data type. Its size is about “8 bytes”, doubling the float data type.
We may face challenges while working with the “double” data type. We can’t print the double data type directly, so we may utilize some techniques to print the complete value of the “double” data type. We can utilize the “setpercision()” method while working with the double data type that contains decimal points. In the other case of the double data type that has exponential values, we can utilize the “fixed” or “scientific” formats. Here, we will discuss the printing of double data types without using any technique and by utilizing all three methods in this guide.
Example 1:
The C++ code is here in which the “iostream” header file is included as we have to work with the functions that are declared in this header file. Then, we place “namespace std” so we don’t need to add the “std” keyword with our functions separately. Then, we invoke the function here which is the “main()” function. In the following, we declare a “double” variable with the “var_a” name and assign a decimal point value to this variable. Now, we want to display this double value, so we utilize the “cout” to place this variable where we store the double value. Then, we add “return 0”.
Code 1:
using namespace std;
int main(void) {
double var_a = 7.9765455419016;
cout << "The double value we placed here = " << var_a;
return 0;
}
Output:
Now, note here in this outcome that it doesn’t print the complete double value that we inserted in our code. So, this is the problem that we face while working with the double data type in C++ programming.
Example 2:
In this example, we will apply the arithmetic operation to the decimal point values and then display the result as a double data type value. We first add the “bits/stdc++.h” header file which includes all the standard libraries. Then, we invoke the “main()” after utilizing the “namespace std”. The “a” variable is declared here with the “double” data type and then assign “1.0/5000” to this variable. Now, it applies this division operation to the data and store the result in the “a” variable of the “double” data type. Then, we display the result that is stored in “a” using “cout”.
Code 2:
using namespace std;
int main(void) {
double a = 1.0/5000;
cout << "My double value is " << a;
return 0;
}
Output:
Here is the result of the given double data type value. We can easily apply the mathematical operations on the values that return the double data type result and display them in our C++ code.
Example 3: Using the Setprecision() Method
Here, we will apply the “setprecision” method. We include two header files: “iosteam” and “bits/stdc++.h”. The “namespace std” is then added which saves us from having to include the “std” keyword with each of our functions individually. The “main()” function is then called below this. The “var_a” variable is now declared with the “double” data type that has a value containing a decimal point in it.
Since we want to display the complete number, we utilize the “setprecision()” function in the “cout” statement. We pass “15” as the parameter of this function. This method aids in setting the number of values of the decimal point in this double data type value. The precision that we set here is “15”. So, it displays “15” numbers of the decimal point value. Then, we put “var_a” in this “cout” after using the “setprecision()” method to print this “double” data type value.
Code 3:
#include<bits/stdc++.h>
using namespace std;
int main(void) {
double var_a = 7.9765455419016;
cout<< setprecision(15) << "The double value we placed here = " << var_a;
return 0;
}
Output:
Here, we can see that the complete value that we entered in the code is displayed. This is because we utilized the “setprecision()” function in our code and set the precision number to “15”.
Example 4:
The “iomanip” and “iostream” are the two header files. The “iomanip” is utilized because the “setprecision()” function is declared in this header file. Then, the “std” namespace is inserted and invokes the “main()”. The first variable of the “double” data type which is declared here is “dbl_1” and the second variable name is “dbl_2”. We assign different values to both variables containing decimal points in them. Now, we apply the same precision number for both values by utilizing the “setpercision()” function and passing “12” here.
Now, the precision number for both values is set to “12 ” which means that these values display “12” values. We utilize this “setprecision()” function after placing the “cout” function. Below this, we print both values of the “double” data type with “cout”.
Code 4:
#include <iostream>
using namespace std;
int main() {
double dbl_1 = 9.92362738239293;
double dbl_2 = 6.68986442623803;
cout << setprecision(12);
cout << "Double Type Number 1 = " << dbl_1 << endl;
cout << "Double Type Number 2 = " << dbl_2 << endl;
return 0;
}
Output:
We might notice that it shows 12 values and ignores all other values of this “double” data type value because we set the precision value in our code.
Example 5:
Here, we declare three variables: “new_d1”, “new_d2”, and “new_d3”. The data type of all three values is “double”. We also assign the values to all these variables. Now, we want to set different precision values for all three variables. We set “15” for the first variable value by passing “15” as the parameter of the “setprecision()” function inside the “cout”. After this, we set “10” as the precision value of the second variable’s value and set “6” as the precision number for this third value.
Code 5:
#include <iostream>
using namespace std;
int main() {
double new_d1 = 16.6393469106198566;
double new_d2 = 4.01640810861469;
double new_d3 = 9.95340810645660;
cout << "Double Type Number with precision 15 = " << setprecision(15) << new_d1 << endl;
cout << "Double Type Number with precision 10 = " << setprecision(10) << new_d2 << endl;
cout << "Double Type Number with precision 6 = " << setprecision(6) << new_d3 << endl;
return 0;
}
Output:
All three values are different here since we adjust the different precision values for all of them. The first value contains “15” numbers since we set the precision value to “15”. The second value contains “10” numbers because of the precision value of “10”, and the third value displays “6” numbers here since its precision value is adjusted to “6” in the code.
Example 6:
We initialize four variables here: two are initialized with the decimal point values and the other two are initialized with the exponential values. After this, we apply the “fixed” format on all four variables by placing them inside the “cout”. Below this, we utilize the “scientific” format on these variables separately by placing them inside the “cout” after using the “scientific” keyword.
Code 6:
#include <iostream>
using namespace std;
int main() {
double my_dbl_1 = 7.7637208968554;
double my_ex_1 = 776e+2;
double my_dbl_2 = 4.6422657897086;
double my_ex_2 = 464e+2;
cout << "By utilizing the fixed keyword " << endl;
cout << "First Double Type Number = " << fixed << my_dbl_1 << endl;
cout << "Second Double Type Number = " << fixed << my_ex_1 << endl;
cout << "Third Double Type Number = " << fixed << my_dbl_2 << endl;
cout << "Fourth Double Type Number = " << fixed << my_ex_2 << endl;
cout << endl;
cout << "By utilizing the scientific keyword:" << endl;
cout << "First Double Type Number = " << scientific << my_dbl_1 << endl;
cout << "Second Double Type Number = " << scientific << my_ex_1 << endl;
cout << "Third Double Type Number = " << scientific << my_dbl_2 << endl;
cout << "Fourth Double Type Number = " << scientific << my_ex_2 << endl;
return 0;
}
Output:
This outcome shows the output after applying the “fixed” and “scientific” formats on the “double” data type values. The “fixed” format is applied on the first four values. On the last four values, the “scientific” format is applied and displays the result here.
Conclusion
The concept of “printing double” data type is discussed in detail here. We explored the different techniques for printing the “double” data type in C++ programming. We demonstrated the three different techniques that aid us in printing the “double” data type values; these are “setprecision()”, “fixed”, and “scientific”. We thoroughly explored all techniques in this guide.