View Single Post
Old 10-11-2005, 08:49 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote