2023-07-01 用C语言把科学计数法转换为普通数字,使用double value; sscanf(str, “%lf“, &value );就可以做到。

一、在科学计数法中,为了使公式简便,可以用带 “E” 的格式表示。例如 1.09乘10的7次方,可简写为 “1.09E+07” 的形式,其中 ”E“ 是 exponent(指数) 的缩写,可以不区分e的大小写,用e或者E都可以。

二、比如+8.12345678E+02 就是 812.345678 ;比如-1.11265976e+05就是 -111265.976

三、测试源码

#include 


int main () {
    int ret;
	char *str;
	double  value;
	float  f_value;
    str = "+2.7625E-01";		
	ret = sscanf(str, "%lf", &value );
	printf("%s,ret=%d,voltage value:%.8lfv %lfv\n",str,ret,value,value);

    str ="-1.11265976E-05";
    value = 0;
	ret = sscanf(str, "%lf", &value );
	printf("%s,ret=%d,voltage value:%.8lfv %lfv\n",str,ret,value,value);
	
    str ="-1.11265976e+05";
	ret = sscanf(str, "%lf", &value );
		printf("%s,ret=%d,voltage value:%.8lfv %lfv\n",str,ret,value,value);
	
    str ="+1.11265976E+05";
	ret = sscanf(str, "%lf", &value );
		printf("%s,ret=%d,voltage value:%.8lfv %lfv\n",str,ret,value,value);
	
	str ="+8.12345678E+02";
	ret = sscanf(str, "%lf", &value );
	printf("%s,ret=%d,voltage value:%.8lfv %lfv\n",str,ret,value,value);
	
	str ="+95961E+03";
	ret = sscanf(str, "%lf", &value );
	printf("%s,ret=%d,voltage value:%.8lfv %lfv\n",str,ret,value,value);
	
	str = "-785.36123575E+01";
	ret = sscanf(str, "%f", &f_value );
	ret = sscanf(str, "%lf", &value );
	printf("%s,ret=%d,voltage value: %lfv,f_value: %fv\n",str,ret,value,f_value);
	
	
	return 0;
}

四、测试结果

标准输出:
+2.7625E-01,ret=1,voltage value:0.27625000v 0.276250v
-1.11265976E-05,ret=1,voltage value:-0.00001113v -0.000011v
-1.11265976e+05,ret=1,voltage value:-111265.97600000v -111265.976000v
+1.11265976E+05,ret=1,voltage value:111265.97600000v 111265.976000v
+8.12345678E+02,ret=1,voltage value:812.34567800v 812.345678v
+95961E+03,ret=1,voltage value:95961000.00000000v 95961000.000000v
-785.36123575E+01,ret=1,voltage value: -7853.612358v,f_value: -7853.612305v

你可能感兴趣的:(c语言,算法,linux)