Home arrow Support arrow Forums

Luminary Micro Forums

ar_mario

Expert Boarder

2007/10/01 03:57

another ftoa.

//one thing that does not like me
//is you have to enabe an array and pointer
//and equal it's address,
//if you want to use this function
//żany sugestion?
//greetings from Spain!!

Code:

  void ftoa(char *ptr,  int decimalsdouble inputDouble){      int ni,nd,k;   long int IntPart;   double DecPart;   int digitsInt[20];   int digitsDec[20];     if (inputDouble<0.0){       *ptr='-';       ptr++;                          //plug singn if necesary       inputDouble = -inputDouble;}      IntPartinputDouble;   DecPart inputDouble-IntPart;   ni=0;   nd=0;        if (IntPart==0) {*ptr='0'ptr++;}    //plug 1st "0" if number is "0.xxxx"   while (IntPart 0){             digitsInt[ni]=IntPart%10;                 //the next digit is the remainder between ineger part and 10     IntPart=IntPart-digitsInt[ni];            //sub this new digit from integer part     if (IntPart>0){IntPart=IntPart/10;}   //div by 10 to remove the units (at this moment units=0)     ni++ ;                                  //go to the next digit                     }     while (nd decimals)  {     DecPart=DecPart*10.0;     digitsDec[nd]=DecPart;     if (digitsDec[nd] > 0){DecPart=DecPart-digitsDec[nd];}     nd++;                           }   //build a ASCII string with decimals array and integers array;   //integer digits must be converted and copied to final string in reverse mode,   //since the first digit is the Least Significant Digit.   //decimal digits can be copied in identical order, following the integer part   if (ni+nd 17){      //if the number to be presented does not fit in a 20 ch     *ptr='N';ptr++;                    //including '-' / '.' /NULL, return an error isntead of crash     *ptr='O';ptr++;                    //i.e. max=17 significant digits, modify as you like     *ptr='F';ptr++;     *ptr='I';ptr++;     *ptr='T';ptr++;}   else{     //copy integer part to final string     for (k=(ni-1); k>-1k--){       *ptr 0x30 +  digitsInt[k];       ptr++;                           }          *ptr='.';         //it's time for a "."      ptr++;          //copy decimal part now.     for (k=0;k<nd;k++){       *ptr =0x30 digitsDec[k];       ptr++;                         }         }    *ptr=NULL//this is the end, my only friend            

login or register to reply

      Topics Author Date
    thread link
another ftoa.
ar_mario 2007/10/01 03:57