How to set the number of exponent numbers in scientific notation in C ++

2

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

#include <stdio.h>

int main(){
    float num=0.0;

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

Exiting:

  

6.0221e + 023

Entry:

  

602214085774747474747474

The correct one:

  

6.0221e + 23

    
asked by anonymous 05.09.2018 / 20:23

1 answer

1

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

#include <stdio.h>

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

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

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

    
05.09.2018 / 20:37