# Thread: display double w/o trailing zeros?

1. ## display double w/o trailing zeros?

I'm trying to display a list of doubles with out the trailing zeros.
This snipet is just the declaration and printf...the actual code is fine
I just need to modify these intergers for the printf
Code:
```   double sq = 0, bb = 0, ac4 = 0, z = 0;
long sq2 = 0, x1 = 0, x2 = 0;
printf("%d+%di, %d-%di.\n\n", x1, sq, x1, sq);```
and keep getting:

1.000000+1.0000000i, 1.0000000-1.0000000i

1+1i, 1-1i

could anyone assist with a method to truncate the trailing zeros?

2. But your code cannot produce that output, %d does not print floats.

3. ## My appoligies

Code:
```   double sq = 0, bb = 0, ac4 = 0, z = 0;
double sq2 = 0, x1 = 0, x2 = 0;
printf("%f+%fi, %f-%fi.\n\n", x1, sq, x1, sq);```
However, the %d was also producing the same result.
Probly my complier.

still getting output w/ trailing zeros

any idea how to truncate the trailing zeros from a float
in printf?

4. http://cplusplus.com/ref/cstdio/printf.html

precision:
for e, E, f types: number of digits to be printed after de decimal point. (if nothing specified default is 6).

5. If you want no decimal digits to be displayed, print as an int:
Code:
```double x = 5.2;
printf("%d\n", (int)x);```
Output:
Code:
`5`

6. I second dwks' response. Or you can use %.nf instead of %f, where the n is the number of decimal places you want displayed. For example, if you want two decimal places it would be %.2f
Note the decimal point(.) placed between the % sign and the number.
-Crazed

7. I like %g.

8. ".nf" can still print with training zeros. An alternative is to print to a string buffer (ensure it at least long enough to hold the value when printed) using sprintf(), strip any training zeros, and then print the resultant string.

9. Code:
```#include<stdio.h>

int main()
{
float u=10.0000;

printf("%.0f",u);

getchar();
return 0;
}
/*myoutput
10
*/```
ssharish2005

10. For comparison.
Code:
```#include <stdio.h>

int main ( void )
{
int i;
for ( i = 1; i < 5; ++i )
{
double d = 1.0 / i;
printf ( "d = %g (%%g) = %f (%%f) = %.0f (%%.0f) = %.2f (%%.2f)\n",
d, d, d, d );
}
return 0;
}

/* my output
d = 1 (%g) = 1.000000 (%f) = 1 (%.0f) = 1.00 (%.2f)
d = 0.5 (%g) = 0.500000 (%f) = 0 (%.0f) = 0.50 (%.2f)
d = 0.333333 (%g) = 0.333333 (%f) = 0 (%.0f) = 0.33 (%.2f)
d = 0.25 (%g) = 0.250000 (%f) = 0 (%.0f) = 0.25 (%.2f)
*/```

11. I did get it to print specific number or decimals using %.nf...

%g does exactly what I want -
Code:
```double sq = 0, bb = 0, ac4 = 0, z = 0;

sq = sqrt(bb - ac4);
z = -0.5*(b + sq);
x1 = z / a;
x2 = c / z;

printf("     Rational roots:     %g\n", x1);
printf("                         %g\n\n", x1, x2);```
this returns no trailing zeros but give the most accuracy for
decimals up to 4 bytes. Thanks for your help everyone

*I'd post the entire code but this is a common homework
problem for intro C programing.