MAX decimal of ftoa (CodeVision)

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi folks!

I need to convert a float to a string:

void C_CALC(void)
{
char i;
float fFINAL_CAPACITANCE;
unsigned char sTEMP_MSG[15];

fFINAL_CAPACITANCE = 1/(6.283*fINDUCTOR_SQUARE*lFREQUENCY);

lcd_gotoxy(0,0);
ftoa(fFINAL_CAPACITANCE,9,sTEMP_MSG);

for(i=0;i<15;i++)
{
lcd_gotoxy(i,0);
lcd_putchar(sTEMP_MSG[i]);
}

        
}

BUT this ftoa above gives me the maximum of 5 decimal points...I needed more....If I use ftoe it gives me the correct aswer, but with that e-9 suffix...

I'd like to know if I am doing something wrong or ftoa gives at maximum 5 decimal points only...

Thank You!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For 32 bit IEEE754 floats (in any C compiler) you get about 8 digits of accuracy - you can't expect to get more - but is float really required here or can you scale some long (or long long) ints ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I can indeed scale some long ints. I asked about the ftoa because in the help of CodeVision it says "The number is represented with a specified number of decimals." , and I tried any number bigger than 5 digits, but it did not have any effect!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you want to have scientific notation, you need to use ftoe() instead of ftoa().

I have tried both, and it seems to give #.######e## regardless of the width parameter.

The alternative is to use the full-featured sprintf().

This should give you the formatting that you require, but with a lot more code in flash.

David.

Edit. the width is only good for 0 to 6 decimal places. Since the float can only represent about 7 or 8 significant figures, there is little point in using a larger width.