Open In App

C++ Program to Convert Scientific Notation to Decimal

Last Updated : 27 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Here, we will see how to convert scientific notations to decimals using a C++ program.

Example:

1e9 = 1000000000

1e9 = 1000000000.000000

Calculate power using the pow(a,b) function 

Header File used: <math.h>

pow also referred to as the power of function. Here, pow(a,b) refers to a power b. Let us check it with an example:

2^3 = 8

pow(2,3) = 8       // Same as 2^3.

Example: 

C++




// C++ Program to implement
// pow function
#include <iostream>
#include <math.h>
  
using namespace std;
  
int main()
{
    int n = 9;
  
    cout << "pow(2,9) : " << pow(2, 9) << endl;
    cout << "pow(10,n) : " << pow(10, n) << endl;
  
    return 0;
}


Output

pow(2,9) : 512
pow(10,n) : 1e+09

In the above Code, Here we calculate 10 to the power n since n is 9 so basically we have calculated 10 to the power 9. But here output is in scientific form. So we will convert it to decimal form.

How to Remove Scientific Notations?

To get a very precise answer in our code we need to solve this issue with scientific notations. Scientific notation is the numbers for which pow returns some value other than decimal or integer and the reason for this is that the answer is too long to be shown as an integer or float.

Methods to remove scientific notation in C++. There are two methods for getting more precise values:

  • Using fixed
  • Using setprecision

1. Using fixed for converting the scientific numbers to decimal

Fixed keyword returns a value to 6 precise values after the decimal. We can’t have that 6 precise number.

Example:

C++




// C++ Program to implement
// fixed function
#include <iostream>
#include <math.h>
using namespace std;
  
int main()
{
    int n = 9;
    cout << "Using pow : " << pow(10, n) << endl;
    cout << "Using fixed : " << fixed << pow(10, n) << endl;
  
    return 0;
}


Output

Using pow : 1e+09
Using fixed : 1000000000.000000

Here we can see we convert the scientific notation to decimal number. But the problem with this is we get 6 zeros after the decimal. So to determine how many zeros are required after decimal we can use setprecision(x).

2. Using the setpercision( ) function for converting the scientific numbers to decimal

Header file used: <iomanip> 
a number of digits after the decimal we want to set can be set with setprecision( ).

Example:

C++




// C++ Program to implement
// setprecision() for 
// get a precised value
#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
  
int main()
{
    int n = 9;
    cout << pow(10, n) << endl;
    
    cout << fixed << pow(10, n) << endl;
    cout << fixed << setprecision(4) << pow(10, n) << endl;
    cout << fixed << setprecision(3) << pow(10, n) << endl;
    cout << fixed << setprecision(1) << pow(10, n) << endl;
  
    return 0;
}


Output

1e+09
1000000000.000000
1000000000.0000
1000000000.000
1000000000.0


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads