char* ftoa(double nb,char* buffer,int digits)
{
  //On corrige le signe
  bool signe;   //vrai si nb<0
  if(nb<0)
  {
    nb*=-1;
    signe=true;
  }
  else
    signe=false;

  //On convertit la mantisse en chaines de caractères.
  int exp=(int)mk_log10(nb);
  if (nb<1)
    exp--;
  nb*=mk_pow10(-exp);

  int i;
  char *p=buffer;
  for (i=0;i<digits;i++)
  {
    *p=(char)((int)nb)+'0';
    p++;
    nb=(nb-(int)nb)*10;
  }

  //On arrondit la fin du nombre (pour virer des 0 et des 9 inutiles).
  int r=(int)nb;
  int nb_chi=digits;
  p--;
  if (r>5)
  {
    while (nb_chi>0&&*p=='9')
    {
      nb_chi--;
      --p;
    }
    if (nb_chi==0)
    {
      nb_chi=1;
      ++p;
      *p='1';
      exp++;
    }
    else
      *p+=1;  //On applique la retenue s'il reste des chiffres.
  }
  else
  {
    while (nb_chi>0&&*p=='0')
    {
      nb_chi--;
      --p;
    }
    if (nb_chi==0)
    {
      buffer[0]='0';
      buffer[1]='.';
      buffer[2]=0;
      return buffer;
    }
  }

  p=buffer;
  if (signe)
  {
    char t,tt;
    t=*p;
    *p='-';
    for (i=0;i<nb_chi;i++)
    {
      p++;
      tt=*p;
      *p=t;
      t=tt;
    }
    p=buffer+1;
  }
  
  if (exp>=0&&exp<nb_chi)
  {
    for (i=0;i<=exp;i++)
      p++;
    char t,tt;
    t=*p;
    *p='.';
    for (;i<nb_chi;i++)
    {
      p++;
      tt=*p;
      *p=t;
      t=tt;
    }
    p++;
    *p=0;
  }
  else if (exp>=nb_chi||exp<0&&(digits<8||exp<-3))
  {
    p++;
    char t,tt;
    t=*p;
    *p='.';
    for (i=1;i<nb_chi;i++)
    {
      p++;
      tt=*p;
      *p=t;
      t=tt;
    }
    p++;
    *p='e';
    itoa(exp,++p,10);
  }
  else
  {
    nb_chi-=exp; //exp<0
    p=buffer+(signe?1:0);
    for (i=nb_chi;i>(-exp);i--)
      p[i]=p[i+exp-1];
    for (;i>=0;i--)
      p[i]='0';
    p[1]='.';
    p[nb_chi+1]=0;
  }
  return buffer;
}

