float to string without ftoa

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
TeMa_1467596
Level 5
Level 5
5 sign-ins 5 likes given First like received

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

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

View solution in original post

0 Likes
5 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes

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

0 Likes

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

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

0 Likes

That depends...

The precision will increase, but the non-exact representation is inherent (as in 1/3) and will stay.

You may #define your own floats and so a change from double to float is easily done.

Bob