**** a simple question ****

Rick Jones rick at tetrauk.UUCP
Tue Sep 18 21:42:53 AEST 1990


In article <793 at babcock.cerc.wvu.wvnet.edu> siping at cathedral.cerc.wvu.wvnet.edu (Siping Liu) writes:
>How can I print out ONLY the significant digits of
>a float number? As you know, "%f" fills in zero's if
>the number does not have enough non-zero digits after
>the point.

Ever tried the "%g" format?

e.g.
	printf ("%g", x);

You can control the number of significant digits with e.g.:

	printf ("%.10g", x);

	note the parameter is significant digits, NOT decimal places

IEEE doubles are not generally reliable beyond 15 sig. digits, so

	printf ("%.15g", x);

	will give you only significant digits, up to the maximum effective
	precision

For very large or very small numbers, this format defaults to the equivalent of
%e or %f respectively.  For more info, read the manual!

-- 
Rick Jones			Nothing ever happens, nothing happens at all
Tetra Ltd.			The needle returns to the start of the song
Maidenhead, Berks, UK		And we all sing along like before
rick at tetrauk.uucp						- Del Amitri



More information about the Comp.lang.c mailing list