Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

361
Visualizações
How to control the number of exponent digits after 'e' in C printf %e?

I want to control the number of exponent digits after 'e' in C printf %e?

For example, C printf("%e") result 2.35e+03, but I want 2.35e+003, I need 3 digits of exponent, how do I use printf?

Code:

#include<stdio.h>
int main()
{
    double x=34523423.52342353;
    printf("%.3g\n%.3e",x,x);
    return 0;
}

Result: http://codepad.org/dSLzQIrn

3.45e+07
3.452e+07

I want

3.45e+007
3.452e+007

But interestingly, I got the right results in Windows with MinGW.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

"...The exponent always contains at least two digits, and only as many more digits as necessary to represent the exponent. ..." C11dr §7.21.6.1 8

So 3.45e+07 is compliant (what OP does not want) and 3.45e+007 is not compliant (what OP wants).

As C does not provide a standard way for code to alter the number of exponent digits, code is left to fend for itself.

Various compilers support some control.

visual studio _set_output_format

For fun, following is DIY code

  double x = 34523423.52342353;
  //                    - 1 . xxx e - EEEE \0
  #define ExpectedSize (1+1+1 +3 +1+1+ 4 + 1)
  char buf[ExpectedSize + 10];
  snprintf(buf, sizeof buf, "%.3e", x);
  char *e = strchr(buf, 'e');  // lucky 'e' not in "Infinity" nor "NaN"
  if (e) {
    e++;
    int expo = atoi(e);
    snprintf(e, sizeof buf - (e - buf), "%05d", expo);  // 5 more illustrative than 3
  }
  puts(buf);

  3.452e00007

Also see c++ how to get "one digit exponent" with printf

over 4 years ago · Santiago Trujillo Relatório

0

printf Format tags prototype:

%[flags][width][.precision][length]specifier

The precision

... This gives ... the number of digits to appear after the radix character for a, A, e, E, f, and F conversions ... .

You are using the conversion and the precision specifier correctly, the difference is with the implementations of the C library function and the environments on the differing systems. The precision specifies the number of digits after the '.' (dot, period, etc..). It does not set the number of characters that represent the exponentiation. The facts that it provides 3 digits on windows is just the way windows specifies the format, not the way the C standard library specifies that printf will work.

It would take comparing how the source implementations differ to see what is relied on for that piece of the format string. (it will probably boil down to some obscure difference in the way the windows v. linux/unix environments/locale/etc. are defined or specified)

over 4 years ago · Santiago Trujillo Relatório

0

char *nexp(double x, int p, int n) // Number with p digits of precision, n digits of exponent.
{
 const int NN=12;
 static char s[NN][256];//(fvca)
 static int i=-1;
 int j,e;

 i=(++i)%NN; // Index of what s is to be used...
 sprintf(s[i],"%.*lE", p,x); // Number...
 for(j=0; s[i][j]; j++) if(s[i][j]=='E') break; // Find the 'E'...
 if(s[i][j]=='E') // Found!
  {
   e= atoi(s[i]+j+1);
   sprintf(s[i]+j+1, "%+0*d", n+1,e);
   return s[i];
  }
 else return "***";
}


// Best Regards, GGa
// G_G
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda