一、在C语言中:
(1)字符型到整型可用ATOI() ATOL(),SSCANF()
(2)整型,浮点型到字符型,可用SPRINTF()
(3)字符型到浮点型用ATOF()不行,最好用SSCANF(),举例如下:
EXEC SQL BEGIN DECLARE SECTION;
double d_demo;
float f_demo;
char ac_text[20]="222";
EXEC SQL END DECLARE SECTION;
(1)sscanf(ac_text, "%f", &d_demo);
printf("ac_text=%s,d_demo=%f ",ac_text,d_demo);
(2)sscanf(ac_text, "%lf", &d_demo);
printf("ac_text=%s,d_demo=%f ",ac_text,d_demo);
(3)sscanf(ac_text, "%f", &d_demo);
printf("ac_text=%s,d_demo=%lf ",ac_text,d_demo);
(4)sscanf(ac_text, "%lf", &d_demo);
printf("ac_text=%s,d_demo=%lf ",ac_text,d_demo);
printf("******************* ");
(5)sscanf(ac_text, "%f", &f_demo);
printf("ac_text=%s,f_demo=%f ",ac_text,f_demo);
(6)sscanf(ac_text, "%lf", &f_demo);
printf("ac_text=%s,f_demo=%f ",ac_text,f_demo);
(7)sscanf(ac_text, "%f", &f_demo);
printf("ac_text=%s,f_demo=%lf ",ac_text,f_demo);
(8)sscanf(ac_text, "%lf", &f_demo);
printf("ac_text=%s,f_demo=%lf ",ac_text,f_demo);
输出的结果:
ac_text=222.00,d_demo=0.000000
ac_text=222.00,d_demo=222.000000
ac_text=222.00,d_demo=222.000032
ac_text=222.00,d_demo=222.000000
*******************
ac_text=222.00,f_demo=222.000000
ac_text=222.00,f_demo=0.000000
ac_text=222.00,f_demo=222.000000
ac_text=222.00,f_demo=0.000000
d_demo=atof(ac_text);
printf("ac_text=%s,atof(ac_text)=%f ",ac_text,d_demo);
d_demo=atof(ac_text);
printf("ac_text=%s,atof(ac_text)=%lf ",ac_text,d_demo);
f_demo=atof(ac_text);
printf("ac_text=%s,atof(ac_text)=%f ",ac_text,f_demo);
f_demo=atof(ac_text);
printf("ac_text=%s,atof(ac_text)=%lf ",ac_text,f_demo);
输出的结果:
ac_text=222.00,atof(ac_text)=1243288.000000
ac_text=222.00,atof(ac_text)=1243288.000000
ac_text=222.00,atof(ac_text)=1243288.000000
ac_text=222.00,atof(ac_text)=1243288.000000
从上面的结果可见:
DOUBLE型应采用sscanf(ac_app_capcity, "%lf", &d_app); 打印用"%lf","%f" 都可以. (2),(4)正确
FLOAT型应采用sscanf(ac_app_capcity, "%f", &d_app); 打印用"%lf","%f" 都可以. (5),(7)正确
采用ATOF()转换的结果都是错的,所以不要用它。
二、写表或从表中取数据时:
(1)字符型与整型之间可不用转换,采用默认方式。
(2)字符型与浮点型之间可不用转换,采用默认方式。
(3)日期型与字符型之间可用TO_CHAR(),TO_DATE()。