c - Floating point formatting in printf() -


I have an array of float where the data is stored with decimal points, so some 123.40000 < / Code> 123.45000 , 123.45600 ... now if I want to print these values ​​without typing 0 in the printf () They are finally without <0>, 123.4 , 123.45 , 123.456 . Is this possible? if so, how?

Use% g formatatter:

  Printf (" G ", 123.4000); 123.4  

123.4

123.4

Trail zero is removed, but unfortunately, decimal is the decimal point, if partial part is zero. I do not know if there is any way of actually doing whatever you want to use printf () - I think that something like this is probably your best bet:

  #include & Lt; Stdio.h & gt; # Include & lt; Math.h> Zero print (FILE * f, double d) {if (d - floor (d) == 0.0) {fprintf (f, "% g.", D); } And {fprintf (f, "% g", d); }} Int main () {Print (stdout, 12.0); Print (standout, 12.300); }  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -