16,551
社区成员
发帖
与我相关
我的任务
分享#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
string format_double(double d);
int main() {
double d = 2.134e+003;
cout << format_double(d) << endl;
return 0;
}
string format_double(double d) {
int dec = 0;
int sign = 0;
char* s = _ecvt(d, 6, &dec, &sign);
int off = 0;
char buf[16] = "";
if(sign) {
buf[off++] = '-';
}
off += sprintf(buf + off, "0.%s", s);
while(buf[off - 1] == '0' && buf[off - 2] != '.') --off;
buf[off++] = 'e';
sprintf(buf + off, "%d", dec);
return buf;
}