>>There has to be a way to do that with a different %e format...
Nope, %e prints extra 0's according to the precision of the data type that you're printing. So if the type has 6 digits of precision and you only fill 2 of them, %e gives you 4 0's. You can get around that by finding out what the precision is that you fill and doing this
Code:
printf("%.*e\n", precision, data);
Or you can take more control over your data by converting it to a string and changing what you need to. The biggest problem with the previous solution is finding what the precision should be for the %e format. The next solution doesn't have that problem :-)
Code:
#include <stdio.h>
#include <ctype.h>
void print_e(float value)
{
char *s;
char *p;
char buff[50] = {0};
/* Stringize so we can work with it */
sprintf(buff, "%e", value);
/* Copy everything except 0's up to the e */
for (s = p = buff; (*p = *s) != 'e'; s++)
{
if (*s != '0')
{
p++;
}
}
/* Copy everything after the e */
while ((*p = *s) != '\0')
{
p++;
s++;
}
/* It's printed nicely :-) */
printf("%s\n", buff);
}
int main()
{
print_e(0.023f);
print_e(0.023900f);
return 0;
}