请问把char变成double用atof,把double变成char用哪个函数呢?
请问把char变成double用atof,把double变成char用哪个函数呢?另外,试用strtod将字符串变成double有精度损失,比如以下的这个程序(我在vc中调试的):
#include <iostream.h>
#include<stdlib.h>
#include <stdio.h>
void main()
{
char *ch1,*ch2;
double dl;
ch1="1445291.278914";
dl=strtod(ch1, &ch2);
cout<<dl<<endl;
}
输出的是:1.44529e+006,也就是1445290,和我想要的数据1445291.278914相差甚远,还有,如果把程序改为ch1="291.278";则显示double数字为291.278,这是为什么呢?有没有不损失精度的转换函数呢?
[解决办法]
double转字符吗?
double a=12345.23567789;
char str[100];
int m=12;//长度
int n=8;//精度
char fmt[10];
sprintf(fmt, "%%%d.%df", m,n);
sprintf(str, fmt, a);
printf("double value is %s\n", str);
[解决办法]
请问把char变成double用atof,把double变成char用哪个函数呢?
呵呵:
#include <stdio.h>#include <stdlib.h>#include <string.h>int main() { char buf[50]; double tt = 123456.456789; sprintf(buf, "%lf", tt); printf(buf); return 0; }