11 years, 2 months ago.

What is the best way of converting a float number to an ASCII string?

What is the best way of converting a float number to an ASCII string?

1 Answer

11 years, 2 months ago.

Probably easiest to just use sprintf:

float value = 1.24;
char buffer[10];

sprintf(buffer, "%f", value);

You can also add precission modifiers the usual way: http://www.cplusplus.com/reference/cstdio/printf/

I would suggest you use snprintf(char *str, size_t size, const char *format, ...) as a rule, slightly less dangerous function.

posted by Chris Pepper 21 Feb 2013