Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
Set precision dynamically using sprintf

Using sprintf and the general syntax "%A.B" I can do this:

double a = 0.0000005l;
char myNumber[50];
sprintf(myNumber,"%.2lf",a);

Can I set A and B dynamically in the format string?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Yes, you can do that. You need to use an asterisk * as the field width and .* as the precision. Then, you need to supply the arguments carrying the values. Something like

  sprintf(myNumber,"%*.*lf",A,B,a);

Note: A and B need to be type int. From the C11 standard, chapter §7.21.6.1, fprintf() function

... a field width, or precision, or both, may be indicated by an asterisk. In this case, an int argument supplies the field width or precision. The arguments specifying field width, or precision, or both, shall appear (in that order) before the argument (if any) to be converted. A negative field width argument is taken as a - flag followed by a positive field width. A negative precision argument is taken as if the precision were omitted.

over 4 years ago · Santiago Trujillo Denunciar

0

Yes - You use "*" e.g.

sprintf(mynumber, "%.*lf", 2, a);

See http://linux.die.net/man/3/sprintf

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda