char * convert_float_to_chars(double f, char * buf, int precision)
{
char * ptr = buf;
char * p = ptr;
char * p1;
char c;
long intPart;
char* temp_str;
// sign stuff
if (f < 0)
{
f = -f;
*ptr++ = '-';
}
f += (double)0.005;
intPart = (long)f;
f -= intPart;
if (!intPart)
*ptr++ = '0';
else
{
// save start pointer
p = ptr;
// convert (reverse order)
while (intPart)
{
*p++ = '0' + intPart % 10;
intPart /= 10;
}
// save end pos
p1 = p;
// reverse result
while (p > ptr)
{
c = *--p;
*p = *ptr;
*ptr++ = c;
}
// restore end pos
ptr = p1;
}
// decimal part
if (precision)
{
// place decimal point
*ptr++ = '.';
// convert
while (precision--)
{
f *= (double)10.0;
c = (char)f;
*ptr++ = '0' + c;
f -= c;
}
}
// terminating zero
*ptr = 0;
temp_str = --ptr;
while(*temp_str != '.')
{
if(*temp_str == '0')
{
*temp_str = '\0';
}
else
{
break;
}
temp_str--;
}
if((*(temp_str+1) == '\0') && (*temp_str == '.'))
{
*(temp_str+1) = '0';
}
return buf;
}
int convert_double_to_chars(char* des, double num, int precision)
{
int temp = 0;
int index = 0;
int val_int = 0;
int tt_temp = 0, factor = 1;
int working_index = 0;
int flag = 0;
double t1 = 0.001, gap = 0.00001;
if(0.00001 > num)
{
flag = 1;
num = num * (-1);
}
t1 = num;
temp = (int)t1;
while(temp > 0)
{
tt_temp = temp % 10;
des[index] = '0' + tt_temp;
++index;
val_int += tt_temp * factor;
temp = temp / 10;
factor *= 10;
}
if(1 < index)
{
--index;
}
// reverse
for(working_index = 0; working_index <= index / 2; ++working_index)
{
temp = des[working_index];
des[working_index] = des[index - working_index];
des[index - working_index] = temp;
}
des[++index] = '.';
++index;
gap = t1 - val_int;
temp = 2;
factor = 10;
while(temp > 0)
{
val_int = (int)(gap * factor);
des[index] = '0' + val_int;
++index;
gap = gap - val_int*1.0/10;
factor *= 10;
--temp;
}
if(1 == flag)
{
for(temp = index - 1; temp >= 0; --temp)
{
des[temp + 1] = des[temp];
}
des[0] = '-';
}
return (index + 1);
}
void convert_chars_to_double(char *str, int *length, double *ret_val)
{
char *temp_str = str;
char working_char = '\0';
int factor = 1, dot_flag = 1, negative_flag = 0, len = 0;
double val = 0.000001, va_small = 0.000001, factor_size = 10.0, small_size = 1.0;
len = 0;
if(NULL != temp_str)
{
working_char = *temp_str;
if('-' == *temp_str)
{
negative_flag = 1;
++temp_str;
++len;
}
}
else
{
return;
}
while('\0' != *temp_str)
{
working_char = *temp_str;
if( ('0'<= working_char) && ('9'>= working_char) )
{
val *= factor_size;
val += working_char - '0';
}
if('.' == working_char)
{
dot_flag = 1;
factor_size = 0.10;
break;
}
++temp_str;
++len;
}
if(1 == dot_flag)
{
++temp_str;
++len;
while('\0' != *temp_str)
{
working_char = *temp_str;
if( ('0'<= working_char) && ('9'>= working_char) )
{
small_size *= factor_size;
va_small += (working_char - '0') * small_size;
}
++temp_str;
++len;
}
val += va_small;
}
if(1 == negative_flag)
{
val *= -1.00000;
}
*length = len;
*ret_val = val;
return;
}
阅读(1804) | 评论(0) | 转发(0) |