By black magic I'm thinking of something like this:
Code:
/*
* bprint() converts a given double value in a way so
* any value less than 1 will _not_ have leading zeroes
*
* buffer - an allocated char*
* where the return value will be stored
* value - the value intended for conversion
* precission - the wanted precission setting, as
* described for any *printf function
*/
#define LENGTH 256
int bprint(char buffer[], double value, unsigned long int precission)
{
char format[LENGTH +1], res[LENGTH +1];
int i=0, j=0;
#ifdef HAVE_SNPRINTF
/* create a format required by the user */
if(!snprintf(format, LENGTH, "%%.%dlf", prescission))
return -1;
/* fill in the value in our prefered format */
if(!snprintf(res, LENGTH, format, value))
return 0;
#else
if(!sprintf(format, "%%.%dlf", precission))
return -1;
if(!sprintf(res, format, value))
return 0;
#endif
if(value < 1.0)
while(res[i]&&res[i]=='0')
++i; /* skip any leading zeroes */
for(;res[i];++i,++j)
buffer[j]=res[i]; /* copy rest of value */
/* nice NULL termination */
buffer[j]='\0';
return 1;
}