读书人

printf打印指数有关问题求解

发布时间: 2012-06-28 15:20:03 作者: rapoo

printf打印指数问题求解
利用printf打印一个指数

double a = 1e+5,

那么利用
printf("%0.1e\n", a);

可以打印出,
1.0e+005

那么如果控制e之后的位数,比如如何打印出
1.0e+05

1.0e+5


[解决办法]

C/C++ code
#include <stdio.h>#include <string.h>void main() {    double a=1e+5;    char s[20];    int L;    sprintf(s,"%0.1le",a);    printf("%s\n",s);//1.0e+005    L=strlen(s);    if (s[L-4]=='+' || s[L-4]=='-') {        if (s[L-3]=='0' && s[L-2]=='0') {            strncpy(s+L-3,s+L-1,2);        } else if (s[L-3]=='0') {            strncpy(s+L-3,s+L-2,3);        }    }    printf("%s\n",s);//1.0e+5} 

读书人网 >C++

热点推荐