两个double转字符数组的函数
时间:2010-07-21 来源:ht2411
因为需要写了个double转字符数组的函数,第一个是修改别人的,第二个是自己写的,呵呵。感觉第一个效果要好写。
本文出处: http://ht2411.cublog.cn
(1)
#include <stdio.h>
long basePower(int base, int i)
{
int ret = 1;
for(;i>0;i--)
ret *=base;
return ret;
}
//function: converts a number into a string
int alpha(char ret[],double value)
{
int num = 0;
int i = 0, j = 0, k = 0;
int index=0;
long nodecimal = 0;
double decimal = 1, valueA = 0;
for(i = 0; decimal != nodecimal; i++)
{
nodecimal = (int)(value*basePower(10, i));
decimal = value*basePower(10, i);
} //i = place counted from right that decimal point appears
valueA = nodecimal; //valueA = value with no decimal point (value*10^i)
for(j = 0; decimal >= 0; j++)
{
nodecimal = (long)(valueA - basePower(10, j));
decimal = nodecimal;
} //j-1 = number of digits
num = j-1;
i--;
j--;
decimal = 0;
for(k = j; k > 0; k--)
{
if(k == i) //if a decimal point was previously found
{ //insert it where its meant to be
ret[index++] = '.';
num++;
}
nodecimal = (long)((valueA - decimal) / basePower(10, k-1));
decimal += nodecimal*basePower(10, k-1);
ret[index++] = (char)('0'+ nodecimal);
}
return num;
}
void main()
{
char ret[20] = {'\0'};
int num = alpha(ret,121234567.8);
printf("%s\n",ret);
printf("%d\n",num);
}
(2)
#include <stdio.h>
int ftos(char *buff, double data)
{
int dec[7];
int i,j,len;
long decimal;
double nodecimal;
decimal = (long)(data);
nodecimal = data-decimal;
for(i=0;decimal!=0; i++)
{
dec[i] = decimal%10;
decimal /=10;
}
len = i;
if(len == 0)
{
*buff = '0';
len++;
}
else
for(;i>0;i--)
*(buff+len-i) = '0'+dec[i-1];
if(nodecimal!=0.0)
*(buff+len++) = '.';
i = 0;
for(;nodecimal!=0.0 && i<6;)
{
nodecimal *=10;
j = (int)nodecimal;
nodecimal -=j;
*(buff+len) = '0'+j;
len++;
i++;
}
return len;
}
void main()
{
char data[15] = {'\0'};
int n = ftos(data,0.100690);
printf("%d %s\n",n, data);
}
本文出处: http://ht2411.cublog.cn
(1)
#include <stdio.h>
long basePower(int base, int i)
{
int ret = 1;
for(;i>0;i--)
ret *=base;
return ret;
}
//function: converts a number into a string
int alpha(char ret[],double value)
{
int num = 0;
int i = 0, j = 0, k = 0;
int index=0;
long nodecimal = 0;
double decimal = 1, valueA = 0;
for(i = 0; decimal != nodecimal; i++)
{
nodecimal = (int)(value*basePower(10, i));
decimal = value*basePower(10, i);
} //i = place counted from right that decimal point appears
valueA = nodecimal; //valueA = value with no decimal point (value*10^i)
for(j = 0; decimal >= 0; j++)
{
nodecimal = (long)(valueA - basePower(10, j));
decimal = nodecimal;
} //j-1 = number of digits
num = j-1;
i--;
j--;
decimal = 0;
for(k = j; k > 0; k--)
{
if(k == i) //if a decimal point was previously found
{ //insert it where its meant to be
ret[index++] = '.';
num++;
}
nodecimal = (long)((valueA - decimal) / basePower(10, k-1));
decimal += nodecimal*basePower(10, k-1);
ret[index++] = (char)('0'+ nodecimal);
}
return num;
}
void main()
{
char ret[20] = {'\0'};
int num = alpha(ret,121234567.8);
printf("%s\n",ret);
printf("%d\n",num);
}
(2)
#include <stdio.h>
int ftos(char *buff, double data)
{
int dec[7];
int i,j,len;
long decimal;
double nodecimal;
decimal = (long)(data);
nodecimal = data-decimal;
for(i=0;decimal!=0; i++)
{
dec[i] = decimal%10;
decimal /=10;
}
len = i;
if(len == 0)
{
*buff = '0';
len++;
}
else
for(;i>0;i--)
*(buff+len-i) = '0'+dec[i-1];
if(nodecimal!=0.0)
*(buff+len++) = '.';
i = 0;
for(;nodecimal!=0.0 && i<6;)
{
nodecimal *=10;
j = (int)nodecimal;
nodecimal -=j;
*(buff+len) = '0'+j;
len++;
i++;
}
return len;
}
void main()
{
char data[15] = {'\0'};
int n = ftos(data,0.100690);
printf("%d %s\n",n, data);
}
相关阅读 更多 +