2

How can I variably influence the number of digits printed in C program?I do not want to write unnecessary zeros in decimal development

x=7 Output: 7

x=7.700 Output 7.7

x=7.77700 Output: 7.777

My point is not to have fixed printf ("%. 3lf", yourVariable); , but but it changed variably on the values ​​I would send there

Aaron7
  • 277
  • 2
  • 10

3 Answers3

3

You can make use of %g Format Specifier in C for this.

Working Fiddle

Working Code

#include <stdio.h>
int main() {   
    float number;
   
    printf("Enter an integer: ");  
    
    // reads and stores input
    scanf("%f", &number);

    // displays output
    printf("You entered: %g", number);
    
    return 0;
}
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • May want to comment that if the values are `double` then `%lf` will be needed for `scanf()`. Worthy of UV for firm understanding of the `g` conversion specifier. – David C. Rankin Oct 18 '21 at 06:05
1

I believe what you are looking for is a way to dynamically change the precision of the output without having to hardcode it.

If you have access to a c99 compiler, you can generate format strings by using snprintf like:

char* format_width(double x, unsigned prec) {
  int fmt_size = snprintf (NULL, 0, "%%.%ulf", prec);
  char* fmt_string = malloc(fmt_size + 1);
  snprintf(fmt_string, fmt_size + 1, "%%.%ulf", prec);
  
  int out_size = snprintf (NULL, 0, fmt_string, x);
  char* out_string = malloc(out_size + 1);
  snprintf(out_string, out_size + 1, fmt_string, x);
  
  free(fmt_string);
  return out_string;
}

Usage could be something like

int main() {   
    double number;
    unsigned prec;
   
    printf("Enter a number: ");  
    
    // reads and stores input
    scanf("%lf", &number);
    
    printf("Enter precision: ");  
    
    // reads and stores precision
    scanf("%u", &prec);
    
    char* result = format_width(number, prec);
    // displays output
    printf("You entered: %s", result);
    
    free(result);
    
    return 0;
}

Demo


Optimizing allocations

In order to avoid too many allocations, one could make use of a buffer of reasonable size to generate the format specifier. For example, it is very unlikely that the fmt_string will ever exceed 15 characters, so we can optimize that part.

It may also be possible that the user already knows the maximum size of input they may ever receive, so we can allow them to pass in the out_string buffer (which is assumed to be large enough to contain the result)

char* format_width(double x, unsigned prec, char* out_string) {
  static char fmt_string[15];
  snprintf(fmt_string, 15, "%%.%ulf", prec);

  int out_size = snprintf (NULL, 0, fmt_string, x);
  snprintf(out_string, out_size + 1, fmt_string, x);
  
  return out_string;
}
smac89
  • 39,374
  • 15
  • 132
  • 179
  • Worthy of UV for `snprintf (NULL, 0 ...` trick for sizing the number of characters needed. But consider passing an array of automatic-storage duration (of reasonable sufficient size) as 3rd parameter to avoid allocating storage and eliminate placing the burden of freeing the storage in case the function is called in a loop. Could become a hot-spot if the user is scanning a large number of floating-point values. – David C. Rankin Oct 18 '21 at 06:11
0

You can simply convert it to a float number

x = (float)x;

IT will remove all the unnecessary 0.

Bahubali
  • 424
  • 1
  • 3
  • 18