Format float number
c, c++
Solution
I don't think there's an internal format like this. You need to format it yourself (not tested):
void fprintf_float(FILE* f, double value) {
if (-1e11 < value && value < 1e11) {
double d = fabs(value);
const char* sign = d > 0 ? "" : "-";
double ipart, fpart;
char fpartstr[16];
int pos;
fpart = modf(d, &ipart);
snprintf(fpartstr, 16, "%.9f", fpart);
for (pos = 10 /*strlen(fpartstr)-1*/; pos > 0; -- pos)
if (fpartstr[pos] != '0' && fpartstr[pos] != '.')
break;
fpartstr[pos+1] = '\0';
fprintf(f, "%s%.11g%s", sign, ipart, fpartstr+1);
} else {
fprintf(f, "%.10e", value);
}
}
Problem
Hi I want to format float numbers such that it will be displayed as follows: decimal.fraction where decimal = max 11 digits and fraction = max 9 digits and if no fraction part it should display not fraction and for more than 11 digits in decimal part representation will be in scientific form. Can anyone help me ?