cancel
Showing results forÂ
Did you mean:Â

# PSoC Creator & Designer Software

Contributor II

## float to string without ftoa

I couldn't make ftoa work and I wanted to control the display format and rounding - then I came across this code snippet and adapted it but it's still not quite what I need.

Can someone please help me tweak it to add a significant digit parameter so that I can specify how many digits after the decimal point?

Here is the routine

`//*****************************************************************************// TM convert float to string// tested and working on PSoC6//*****************************************************************************void tmFtoStr(char* p, float x){    int n,i=0,k=0;    n=(int)x;    while(n>0)    {        x/=10;        n=(int)x;        i++;    }    *(p+i) = '.';    x *= 10;    n = (int)x;    x = x-n;    while((n>0)||(i>k))    {        if(k == i)            k++;        *(p+k)='0'+n;        x *= 10;        n = (int)x;        x = x-n;        k++;    }    /* Null-terminated string */    *(p+k) = '\0';}`

I call it like this

`int main(void) {    float flt1;    char myString[20]={};    flt1 = 3.142;    tmFtoStr(myString, flt1);    printf("%s\n", myString);.......}`

but the print out from the above is like this...

3.141999721527

I think I know what to do but I am scared that I'll break it -  I want to add a sigDig parameter so I call it like this...

tmFtoStr(myString, flt1, 3);

and get 3.142 - ideally doing rounding if possible.  Any help would be appreciated.

Also, is the problem above because of the inability of floats to store precisely?

Ted

1 Solution
Esteemed Contributor II

## Re: float to string without ftoa

You may use sprintf() function to convert any variable to string.

Requires: Set the heap size to 0x0200 (System view) and allow newlib nano float formatting (Build settings)

Usually double is more precise than float

Bob

5 Replies
Esteemed Contributor II

## Re: float to string without ftoa

You may use sprintf() function to convert any variable to string.

Requires: Set the heap size to 0x0200 (System view) and allow newlib nano float formatting (Build settings)

Usually double is more precise than float

Bob

Contributor II

## Re: float to string without ftoa

Hi Bob,

I tried sprintf but could not make it work, at first - where do I change heap size?  There's no heap size under the system tab in CPS_Pinout.cydwr

I changed to use NewLib nano, recompiled and sprintf now works so you have solved my problem although I lost about 5k of flash, went from about 44k to 49k usage in my build output report.

We could look at the code above if you were feeling masochistic?

Thanks,

I'm curious, what difference does NewLibNano make?

Ted

Esteemed Contributor II

## Re: float to string without ftoa

Newlib nano formats any of the sprintf() parameters which is a bit more than your code is able to do.

float numberscannot always be represented without a difference. The reason is that the mantissa has to be built using fractions of powers of 2. As in the decimal world 1/3 cannot be represented without error is in the hex world 342/100

Bob

Contributor II

## Re: float to string without ftoa

Thanks, would I be better off using a double rather than a float then? It just uses 8 bytes instead of 4 correct?

Esteemed Contributor II