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

202
Visualizações
print double in scientific format with no integer part

A simple problem but I can't get documentation about this kind of format: I want to print a float in a Fortran scientific notation, with its integer part always being zero.

 printf("%0.5E",data); // Gives 2.74600E+02

I want to print it like this:

 .27460E+03

How can I get this result as clean as possible?

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

0

I tried doing this with log10() and pow(), but ended up having problems with rounding errors. So as commented by @Karoly Horvath, string manipulation is probably the best approach.

#include <stdlib.h>

char *fortran_sprintf_double(double x, int ndigits) {
  char format[30], *p;
  static char output[30];

  /* Create format string (constrain number of digits to range 1–15) */
  if (ndigits > 15) ndigits = 15;
  if (ndigits < 1) ndigits = 1;
  sprintf(format, "%%#.%dE", ndigits-1);

  /* Convert number to exponential format (multiply by 10) */
  sprintf(output, format, x * 10.0);

  /* Move the decimal point one place to the left (divide by 10) */
  for (p=output+1; *p; p++) {
    if (*p=='.') {
      *p = p[-1];
      p[-1] = '.';
      break;
    }
  }

  return output;
}
over 4 years ago · Santiago Trujillo Relatório

0

If you only care about the integer part being 0 and not really leaving out the 0, i.e. if you're fine with 0.27460E+03 instead of .27460E+03 you could do something similar to this:

#include <stdio.h>
#include <stdlib.h>

void fortran_printf();

int main(void)
{
        double num = 274.600;
        fortran_printf(num);

        exit(EXIT_SUCCESS);
}

void fortran_printf(double num)
{
        int num_e = 0;
        while (num > 1.0) {
                num /= 10;
                num_e++;
        }

        printf("%.5fE+%02d", num, num_e);
}

Otherwise you have to take a detour over strings. Note that the code above is only meant to get you started. It certainly doesn't handle any involved cases.

over 4 years ago · Santiago Trujillo Relatório

0

A string manipulation approach:

int printf_NoIntegerPart(double x, int prec) {
  char buf[20 + prec];
  sprintf(buf, "%+.*E", prec - 1, x * 10.0);  // use + for consistent width output
  if (buf[2] == '.') {
    buf[2] = buf[1];
    buf[1] = '.';
  }
  puts(buf);
}

int main(void) {
  printf_NoIntegerPart(2.74600E+02, 5);  // --> +.27460E+03
}

This will print "INF" for |x| > DBL_MAX/10

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