"Peter Nilsson" <(E-Mail Removed)> wrote in message

news:(E-Mail Removed) oups.com...

> Mike Wahler wrote:

> >

> > #include <math.h>

> > #include <stdio.h>

> >

> > int main()

> > {

> > float foo = 6.180f;

> > double ipart = 0;

> > double frac = modf(foo, &ipart);

> > int ifrac = 0;

> > char s[30] = {0};

>

> round.c:10: warning: unused variable `s'
I meant to take that out.

>

> > float adj = 0.005f; /* for rounding */

>

> ITYM 0.5f
No. We're rounding to nearest thousand, not one.

OF course this has the 'fragility' that it doesn't

handle other numbers of wanted digits after the

decimal point. But that's OP's problem.

>

> >

> > printf("floating point value: %.3f\n"

> > "integer portion: %.3f\n"

> > "fractional portion: %.3f\n",

> > foo, ipart, frac);

> >

> > ifrac = (int)(frac * 1000 + adj);

>

> This is somewhat simplistic, i.e. it may not round as expected if foo

> (and thus possibly frac) is negative.
Yes, it is simplistic. Had not the OP specifically disallowed it,

I would have advised creating a formatted string, parsing that

for the '.', and converting the desired subsequent characters

to type 'int'.

>

> > printf("adjusted result: %d\n", ifrac);

> >

> > return 0;

> > }
-Mike