how do i print a float number without extra zero digits in c?

There are many ways to remove the extra zeros.

  1. Easiest way is to use %g as the format specifier in the printf statement. Eg. printf("%g",a/b);. This is remove the extra zeros after the decimal.

  2. You can use printf("%.0d%.4g\n", (int)(a/b)/10, (a/b)-((int)(a/b)-(int)(a/b)%10));.

  3. You can use a separate function to remove the extra zeros but it is a tedious process. First transfer all the digits to a string and remove the unwanted zeros from the end.

  4. You can also use %.2f format specifier to reduce the zeros. Eg. printf("%.2f",a/b); – this can be used where you want to print the exact number of digits after the decimal point. %.<the number of digits to be printed after decimal point>f.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top