首页 > 代码库 > 将浮点数转换为字符串

将浮点数转换为字符串

char *F2S(double d, char *str) {
char str1[40];
int j = 0;
int k, i;
i = (int)d; // 浮点数的整数部分
while (i > 0) {
str1[j++] = i % 10 + ‘0‘;
i /= 10;
}
for (k = 0; k < j; ++k) {
str[k] = str[j - 1 - k];
str[j++] = ‘.‘;
for (i = 0; i < 10; ++i) {
d *= 10;
str[j++] = (int)d + ‘0‘;
d -= (int)d;

}
}
while (str[--j] == ‘0‘) {
str[++j] = ‘\0‘;
return str;
}
}

将浮点数转换为字符串