Abe Simpson wrote:

>

> Hello all,

>

> The application I am working on must never output numbers in a

> floating-point format, that is, something like

>

> 2e-002

>

> is a big no-no. At the same time, it must output numbers in a compact way,

> that is, it should only output significant digits, and only output a decimal

> point if there are digits following it.

>

> There seems to be no printf modifer to accomodate me.

>

> %f is fixed-point but not compact:

>

> printf("%f", 222) produces 222.000000 and I only need 222
Nit-pick: printf("%f", 222) produces undefined behavior,

because `222' is not a `double' quantity as required by "%f".

> %g is compact but not fixed-point:

>

> printf("%g", 0.00001) produces 1e-005 and I want 0.000001

>

> Any way I can do this with printf alone without writing my own 0-trimming

> code?
You need to decide how many digits to the right of the

decimal point you want. This will depend on the size of the

number (the log10() function may come in handy here) and on

your notion of how many digits are "significant." Armed with

this information you can then choose an appropriate precision

for the conversion, which you'll probably write as

printf ("%.*f", precision, value);

As an aside, note that printing values with very large or

very small magnitudes is not especially "compact."

-1e37 = -10000000000000000000000000000000000000

1e-37 = 0.0000000000000000000000000000000000001

.... and you don't even want to *think* about 1e308 ...

--

http://www.velocityreviews.com/forums/(E-Mail Removed)