Arduino Float to String

If you have ever tried to use sprintf() on an Arduino to convert from a float to a string, you will notice it doesn't work.

sprintf(buf,"%f", floatvar);

The above function will most likely return a "?" to your char buffer.

If you google around, you'll see various functions that people have written to overcome this, but all of them seem broken in one way or another. The alternative is to use dtostrf(), a standard avr-libc function. This provides a simple, tested function to convert from a float to a string.

To use this function, simply replace sprintf() with dtostrf(), along with the self explanatory parameters below.

dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);

As the function name suggests, this will also work for the conversion of doubles to strings.

36 thoughts on “Arduino Float to String

  1. 2011年10月25日のつぶやき « 雑記帳

  2. Thank you very much about this information. I battled with that sprintf() function whole day and wondered why the hell it isnt working. But finally now my lcd shield shows temperature...

  3. Confusion.com.mx » Convirtiendo datos flotantes a cadenas (Arduino)

  4. Confusión - Convirtiendo datos flotantes a cadenas (Arduino)

  5. Very helpful! This solved the mysterious sprintf() problem I was observing on my arduino. Many thanks indeed!

  6. Thanks Jon for the example.
    What determines the value in

    static char dtostrfbuffer[0];

    I have tried 0,1,10,100 and they all seem to work just fine.

    • Hi Butch,

      dtostrfbuffer is a char array containing the string representation of the floating point number, floatvar.

      Jon

  7. okay maybe you can help me, do you know why if I write

    line 1: dtostrf(float1,0,2,variable1);
    line 2: dtostrf(float2,0,2,variable2);
    line 3: dtostrf(float3,0,2,variable3);

    variable2 becomes nothing. Some how line 3 deletes variable2 data.

  8. Thanks for the tip. How would you put more than 1 variable into dtostrf(). For example, I currently have sprintf(buf, "%d,%d,%d", sensor1, sensor2, sensor3); and sensor 1 will be a float and the other 2 will be integers. Sensor 1 would never be more than 99.99.

    Thank you.

    • Hi Todd,

      dtostrf() only allows a single float input argument. You could pass the output of that conversion as a string input to your sprintf line, or you could manipulate the memory manually and create the desired string using memcpy(). See this gist as an example.

      Jon

  9. Qualcuno sperimenta con Arduino? - Pagina 16 - BaroneRosso.it - Forum Modellismo

  10. Hello

    I wrote the following based on your barebones example
    given above.

    static char dtostrfbuffer[15];

    void setup()
    {
    Serial.begin(9600);
    }

    void loop()
    {
    for (float a = 1.40 ; a <=1.60 ; a+=0.01)
    {
    Serial.print(a,6);
    Serial.print(" ");

    // round the float
    float b = a * 100;
    int c = round(b);
    float d = c/100.0;

    Serial.print(b,6);
    Serial.print(" ");
    Serial.print(c);
    Serial.print(" ");
    Serial.print(d,6);
    Serial.print(" ");

    dtostrf(d, 4, 1, dtostrfbuffer);
    Serial.println(dtostrfbuffer);

    }
    }

    //
    // sample print out from above code
    // (headers [i.e. 'a', 'b', etc.] added by hand)
    //

    a b c d println

    1.420000 141.999954 142 1.420000 1.4
    1.430000 142.999954 143 1.430000 1.4
    1.440000 143.999954 144 1.440000 1.4
    1.450000 144.999954 145 1.450000 1.5 z
    1.460000 145.999954 146 1.460000 1.5
    1.470000 146.999954 147 1.470000 1.5
    1.480000 147.999954 148 1.480000 1.5
    1.490000 148.999954 149 1.490000 1.5
    1.500000 149.999954 150 1.500000 1.5
    1.509999 150.999954 151 1.510000 1.5
    1.519999 151.999954 152 1.520000 1.5
    1.529999 152.999954 153 1.530000 1.5
    1.539999 153.999954 154 1.540000 1.5
    1.549999 154.999954 155 1.550000 1.5 w
    1.559999 155.999938 156 1.560000 1.6
    1.569999 156.999938 157 1.570000 1.6
    1.579999 157.999938 158 1.580000 1.6

    Question:
    Why is 'z' correctly rounded to 1.5, but 'w' is not rounded to 1.6?
    This is the same problem I found when I used:
    Serial.println(a,1);

    Thanks

    Ron

    • Hi Ron,

      I can confirm same behaviour.

      Looks like a bug. I'll dig a little deeper to see can I find any more on this.

      Jon

  11. Arduino Float to String (That actually works) | Protoneer.co.nz

  12. And talking the talk… | The Pendaqueous Sensor Pod

  13. What’s running in the currently deployed prototypes | An Arduino Based Underwater Sensor Pod

  14. Leaner meaner code for the Pearl. | An Arduino Based Underwater Sensor Pod

  15. Updated SRAM buffering | An Arduino Based Underwater Sensor Pod

Leave a Reply