How to convert Float To String?

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

I'm using Atmel Studio 6.2.
One of my project i'm need to Convert a Float value to String.

Searching on google but Failed to find useful something.

 

Dose WinAvr Compiler have any Lib like (atoi) to convert Float to String?

 

----------

Regards,

eRony 

শূন্য  - The ZeRo

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

 

 

Check this out:

http://www.geeksforgeeks.org/con...

 

Also look up SPRINTF

http://www.tutorialspoint.com/c_...

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

dtostrf()

 

Use <stdlib.h>

 

Make sure the build links with libm.a

 

(the above for GCC)

 

Alternatively every C compiler on planet earth surely knows about sprintf("%f") ?

Last Edited: Fri. Nov 13, 2015 - 04:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear sir,
i know the sprintf();

 

but you know that i'm coding for MCU.
itz not PC.

 

Thanks :)

and you can use the suggested commands for the MCU

 

JIm

 

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

eRony wrote:
but you know that i'm coding for MCU.
itz not PC.
So?

 

How about you start with the documentation?

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

You are the one that said "float". No one coding for MCUs normally goes anywhere near float. If you do it costs an instant 1K. If you then want to print (ie convert to ASCII) them that "costs" a further 2K.

 

As I say dtostrf() is a "lightweight" way to get a float converted to ASCII characters.

 

Here is an example. It's completely fictious and doesn't make a lot of sense but kind of shows the "costs" involved:

$ cat avr.c
#include <avr/io.h>

int main(void) {
  float f;
  f = 3.14 * PINB;
  PORTC = f;
  while(1);
}
$ avr-gcc -Os -mmcu=atmega16 avr.c -o avr.elf -lm
$ avr-size avr.elf
   text	   data	    bss	    dec	    hex	filename
    718	      0	      0	    718	    2ce	avr.elf

So just including a floating point multiply cost about 600..700 bytes. If I then use dtostrf() I get:

$ cat avr.c
#include <stdlib.h>
#include <avr/io.h>

char buffer[20];

int main(void) {
  float f;
  f = 3.14 * PINB;
  dtostrf(f, 6, 4, buffer);
  PORTC = buffer[0];
  while(1);
}
$ avr-gcc -Os -mmcu=atmega16 avr.c -o avr.elf -lm
$ avr-size avr.elf
   text	   data	    bss	    dec	    hex	filename
   2208	      0	     20	   2228	    8b4	avr.elf

So the float->ASCII stuff has "cost" an additional 1,500 bytes. If I go with sprintf() then it is:

$ cat avr.c
#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>

char buffer[20];

int main(void) {
  float f;
  f = 3.14 * PINB;
  sprintf(buffer, "%f", f);
  PORTC = buffer[0];
  while(1);
}
$ avr-gcc -Os -mmcu=atmega16 -Wl,-u,vfprintf avr.c -o avr.elf -lm -lprintf_flt
$ avr-size avr.elf
   text	   data	    bss	    dec	    hex	filename
   3736	      4	     20	   3760	    eb0	avr.elf

On the other hand you only pay the sprintf() cost once and it is a far more utilitarian function. But if space is tight and you insist on using "float" in an MCU then stick to dtostrf() and pay the 1,500 byte cost.