Friday, May 05, 2006

Convert Floating Point to String

A few months ago, I found out that there is no built-in function for converting a floating point data type variable to a string although there are functions that do the following:

1. Convert a string to floating point (atof)
2. Convert an integer to string (itoa)
3. Convert a string to an integer (atoi)

All the above functions come in very handy when you want to write your data to a text file. It is quite intriguing because itoa(), atoi() and atof() are provided but ftoa() are not. Unfortunately, many data are floating points in nature. Also, data that are larger than a 32-bit integer are usually represented as floating point data type. Anyway, many programming experts have already provided ftoa() solution on their websites. I believe it is not difficult to find one out there but I would like to have my very own one, too, as it is no harm trying. The C/C++ code is as shown below. I have tested it in the Microsoft Visual C++ compiler but I do not cover all the possible cases. So, use it on your own risk. It is not the best solution available for ftoa() but it certainly enough to meet my purposes. If you need one, I hope that it helps you, as well. If not, you can always write one for yourself.


////////////////////////////////////////////////////////////////
// my_ftoa.cpp
// date created: May 5, 2006
// author:
http://fpgaforum.blogspot.com/
////////////////////////////////////////////////////////////////


#include <iostream>
#include <cmath>
using namespace std;
#define PRECISION_POINT 4 //Change this to display more or less precision point

void ftoa(float f, char* a, int point); //function prototype

void main()
{
  float f = -1.23456;
  char result[32];
  ftoa(f,result, PRECISION_POINT);
  cout << result;
}

void ftoa(float f, char* a, int point)
{
  char buf_int [2]; //temporary buffer
  char buf_d [16]; //to store the integral part, e.g. "-123" for "-123.456"
  char buf_f [16]; //to store all the fraction part, e.g. "000123" for "1.000123"
  char buf_nz [16]; //to store the non-zero fraction part, e.g. "123" for "1.000123"
  int d= (int)(f); //integral part

  float fp = (f<0) ? (d-f) : (f-d); //fractional part

  int fp2int = fp*(pow(10,point));
  int fpplus1 = fp*(pow(10,point+1));

  if((fpplus1%10)>=5)
    fp2int++;

  strcpy(buf_int,"");
  strcpy(buf_d,""); //clear the buffer, just to be safe
  strcpy(buf_f,""); //clear the buffer, just to be safe
  
strcpy(buf_nz,""); //clear the buffer, just to be safe

  if((d == 0) && (f<0) && (f>-1))
  {
    strcat(buf_d,"-");
    itoa(d,buf_int,10);
    strcat(buf_d,buf_int);
  }
  else
  {
    itoa(d,buf_d,10);
  }
  strcat(buf_d,".");
  //looking for the leading zeros at the fractional part
  if(fp2int == 0)
  {
    for(int i = 0; i < point-1; i++)
      strcat(buf_f,"0");
  }
  else if(fp2int < (pow(10,point-1)))
  {
    for(int i = 0; i < point; i++)
    {
      int j = pow(10,(point-1-i));
      if(fp2int < j)
        strcat(buf_f,"0");
      else break;
    }
  }

  itoa(fp2int,buf_nz,10);
  strcat(buf_f,buf_nz);
  strcat(buf_d,buf_f);
  strcpy(a,buf_d);
}

// The end
////////////////////////////////////////////////////////////////

3 comments:

Anonymous said...

if you want to go from float to string try out sprintf

char somestring[20];
memset(somestring, 0, 20);
float blah=1.1;
sprintf(somestring,"%f",blah);



=)

Anonymous said...

Nice post and this post helped me alot in my college assignement. Say thank you you on your information.

Anonymous said...

Amiable brief and this post helped me alot in my college assignement. Say thank you you seeking your information.