# Conversione float to string

Bonjour,

I'm using the following function to convert a float to a string:

``````String ftoa(float number, uint8_t precision, uint8_t size) {
// Based on mem,  16.07.2008
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num = 1207226548/6#6

// prints val with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimial places
// example: printDouble(3.1415, 2); // prints 3.14 (two decimal places)

// Added rounding, size and overflow #
// ftoa(343.1453, 2, 10) -> "    343.15"
// ftoa(343.1453, 4,  7) -> "#      "
// avenue33, April 10th, 2010

String s = "";

// Negative
if (number < 0.0)  {
s = "-";
number = -number;
}

double rounding = 0.5;
for (uint8_t i = 0; i < precision; ++i)    rounding /= 10.0;

number += rounding;
s += String(uint16_t(number));  // prints the integer part

if(precision > 0) {
s += ".";                // prints the decimal point
uint32_t frac;
uint32_t mult = 1;
while(precision--)     mult *= 10;

frac = (number - uint16_t(number)) * mult;

uint32_t frac1 = frac;