-2
#include <iostream>
#include <iomanip>

using namespace std;
int main()
 {
    long double n;
    cin>>n;
    cout<<setprecision(4)<<fixed<<scientific<<n;

    return 0;
}

For Example : If I input something like 256 in the program,I get 2.5600e+002 as output.But I want to print 2.5600*(10^2).I also want to be able to control how many digits gets displayed after the decimal point.

Can anybody help me with that?

lolplayer
  • 21
  • 1
  • 2
  • 4
    `I want to print 2.5600*(10^2)` then implement a function that prints it like that. The `2.5600e+002` output is correct. [E-notation](https://en.wikipedia.org/wiki/Scientific_notation#E-notation) looks like a valid notation for scientific notation. Note that both `fixed` and `scientific` print the number in hexadecimal format. – KamilCuk Oct 12 '19 at 21:51
  • 2
    `setprecision` controls the number of decimal places; as @KamilCuk says, if you want a different format, you can write your own. – Scott Hunter Oct 12 '19 at 21:54
  • I know the answer is correct but I want to format it like they do on textbooks. – lolplayer Oct 12 '19 at 21:55
  • @ScottHunter ..OK,I'll use setprecision but how do I write a different format? – lolplayer Oct 12 '19 at 21:59

1 Answers1

0

As mentioned in the comments, the notation is correct for scientific notation. If you want it printed your way, you will have to make a special function for it, like this:

void PrintScientific(long double d) {
    int e = 0;
    if(d < 1 && d > -1) {
        for(e = 0; d*10 < 10 && d*10 > -10; e--) {
            d *= 10;
        }
    } else {
       for(e = 0; d/10 > 1 || d/10 < -1; e++) {
            d /= 10;
        }

    }

    std::cout << std::setprecision(4) << std::fixed << d << "*10^" << e;
}

This might not be exactly what you need, but should get you started.