Here's yet-another-version of the FloatToString function posted to this
group. It's not perfect (e.g. it does not handle large floats gracefully),
but it's a start. I don't know who's the original author, but many thanks
to him/her. I've added a fix (kludge?) to handle the case where rounding
propagates into the integral portion of the result. I also modified it so
that it returns a pointer to the internal buffer that holds the result,
rather than copying the result into a buffer provided as an arg. If you
find a better solution, please let us all know...
Miguel Oyarzun
P.S. If you want more of the history ot this function, search the group
archive for FloatToString...
P.P.S. Insert standard disclaimer about how this code is provided as is,
with no claims of correctness implied, and how if you use it and your
unmanned spaced vehicle fails to go into orbit because of it, it's not my
problem, etc, etc, etc...
char *FloatToString (double value, int round)
{
static char sResult[50];
static char sDecimal[50];
char *sTemp = sResult;
long iValue;
double dDecimal;
long iDecValue;
long iTemp;
int i;
iValue = value;
if( value < 0 )
{
dDecimal = -(value - iValue);
// Make sure that negative #s where -1 < n < 0
// get handled correctly.
if( 0 == iValue )
StrCopy( sTemp++, "-" );
}
else
dDecimal = value - iValue;
if (dDecimal < 0.000000001) dDecimal = 0;
// Round decimal portion
for (i = 1; i <= round; i++)
dDecimal = dDecimal * 10;
iDecValue = dDecimal;
if (dDecimal - iDecValue >= 0.5) iDecValue++;
// Handle possible carry into integer portion
iTemp = iDecValue;
for (i = 1; i <= round; i++)
iTemp = iTemp / 10;
if (iTemp == 1)
{
iValue += (iValue >= 0) ? (1) : (-1);
for (i = 1, iTemp = 1; i <= round; i++)
iTemp = iTemp * 10;
iDecValue = iDecValue - iTemp;
}
// Convert integer portion to string
StrIToA( sTemp, iValue);
if (StrLen(sTemp) < 1) StrCopy(sTemp, "0");
StrCat(sResult, ".");
// Add decimal portion
StrIToA (sDecimal, iDecValue);
// Add leading zeros if neccessary
if (StrLen (sDecimal) < round)
for (i = 1; i <= round - StrLen(sDecimal); i++)
StrCat (sResult, "0");
StrCat (sResult, sDecimal);
return sResult;
}
"Jay" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]
>
> Of course this is the result from a computation and I would like to be
able
> to print the results in a textfield.
>
> Jay
>
>
> "Jay" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]
> >
> > Is it me? I am simply trying to print out in a textfield the number
> 0.03421
> > and I can't seem to do it.
> > Can someone help?
> >
> > Jay
> >
> >
> >
> >
>
>
>
>
--
For information on using the ACCESS Developer Forums, or to unsubscribe, please
see http://www.access-company.com/developers/forums/