How to configure the amount of exponent numbers in scientific notation in C++

Asked

Viewed 826 times

2

My code is giving the error in the presentation of the scientific number, because it always appears with 3 decimal places being necessary only.

#include <stdio.h>

int main(){
    float num=0.0;

    scanf("%f",&num);
    printf("%.4e",num);
    return 0;
}

It’s coming off:

6.0221e+023

Entree:

602214085774747474747474

Right:

6.0221e+23

1 answer

1


Everything is ok in compiler that works according to the specification.

#include <stdio.h>

int main() {
    float num;
    scanf("%f", &num);
    printf("%.4e\n", num);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you are using a compiler that does wrong you need to see how it offers a solution, if it offers (otherwise, you have to accept it anyway). If you are the Microsoft compiler you have to use this: _set_output_format() to configure.

  • that’s right, I was using windows. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.