dtostrf() – turn your floats into strings

dtostrf() may be the function you need if you have a floating point value that you need to convert to a string.

In this lesson you will learn exactly how to use dtostrf in your Arduino code.

dtostrf() syntax

Let’s jump straight into this. Here are the parameters that dtostrf() expects:

dtostrf(float_value, min_width, num_digits_after_decimal, where_to_store_string)
dtostrf syntax

The first value is the floating point number that you want to convert into a string – that’s easy enough.

dtostrf first parameter

minimum field width

The second value is the minimum field width.

If you set the minimum field width to 6, and you convert a floating point value that has greater than 6 digits, those extra digits will still display. And for counting purposes, the “.” decimal point and the “-” negative sign count as spaces too.

If you set the minimum field width to 6, and you convert a floating point value that has less than 6 digits, then spaces will be added before the number starts.

dtostrf second parameter

The minimum field width can also be negative. If you pass dtostrf() a negative value, it will left-justify the number within the field width.

dtostrf second parameter negative left-justifies value

precision

The 3rd argument passed to dtostrf() is the precision, which is the number of digits to show after the decimal point. If the floating point value you convert to a string has more digits after the decimal point than the number specified in the precision, then it will be cut off and rounded accordingly in the output string.

If you pass a number with fewer digits after the decimal point, it will add trailing zeros to the number.

dtostrf third parameter

dtostrf() char buffer sizing

The final argument passed to dtostrf() is where you want to store the output string. This will typically be a character array, like buffer[7]. When determining the size of this buffer, make sure to consider:

  • What the biggest number might ever be
  • Size must include space for the “.” and the possible “-” sign
  • Add 1 for the null-terminating character “\0”
dtostrf final parameter

Let Us know

What are you using dtostrf() for?  We’d love to know – tell us in the comments! Also, if you found this useful, you might also find our sprintf() lesson super handy as well.

AppLab Bricks open in background with actual brick

Arduino AppLab Bricks → Marketing Garbage or New Powerful Interface?

Arduino Ventuno single board computer - top side

New Ventuno Q Dual Brain Single Board Computer

AppLab Pip Install

How to Add Python Packages in Arduino AppLab (No pip install needed)

Arduino Power Section Schematic

Kit-on-a-Shield Schematic Review

Just how random is the ESP32 random number generator?

Just how random is the ESP32 random number generator?

16 Comments

  1. KEITH CHARLES LOHMEYER on June 19, 2021 at 12:05 pm

    Great to see you teaching these little documented C++, Arduino formatting functions. You only missed that you can add label and units to the formatted data. I use this example sketch in my classes when we study printing to LCD and other displays.

    char str[25]; //at least as long as # of char +1
    float f = 98.6746;
    void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    Serial.println(f, 3); //formats to number of decimal places-no leading blanks
    //dtostrf example
    //set value of f to char str
    //f is float or int var, 7 is the # of char in result
    //1 is number of dec places shown, str is result
    //creates leading spaces that cover old data
    dtostrf(f, 7, 0, str);
    Serial.println(str);

    //you can also combine label and data together
    //for ease in printing to tft display
    strcpy(str, “Temperature: “);
    dtostrf(f, 7, 2, &str[strlen(str)]); //add to end of str
    Serial.println(str);
    strcat(str, “F”);
    Serial.println(str);

    // only ints
    int h = 74;
    sprintf(str, “Humidity:%5d%%\n”, h);
    Serial.println(str);

    }

    void loop() {
    // put your main code here, to run repeatedly:

    }

  2. KEITH CHARLES LOHMEYER on June 19, 2021 at 12:36 pm

    I see now you covered the text, label, and units in the sprintf() video. Either will work. The advantage above is you only need one char array. Keep up the good work.

  3. pakainfo on August 20, 2021 at 3:26 am

    As by default sprintf does not support floats in a standard Arduino environment, there’s dtostrf() coming with avr-gcc, which does what you want.

  4. Howard Bussey on August 29, 2022 at 9:42 am

    I use dtostrf to print (millis()/100)/10.0 — seconds and tenths. I could always “a=millis(); Serial.print(a/1000); Serial.print(‘.’); Serial.print((a%1000)/100)); …”. dtostrf does not appear to be in the library for a MKR wifi 1010 board, though.

    • Michael James on August 29, 2022 at 3:32 pm

      Love this – thanks for adding Howard! I’ll have to give this a try.

  5. Tony on October 31, 2022 at 11:22 am

    Great article.

    The only thing I see missing in dtostrf is the option to have a explicit + sign in front of the converted float to match the – sign generated for negative numbers. That would be instead of just have a space in that position in the output string for positive numbers.

  6. Pen on December 9, 2023 at 2:52 pm

    I keep seeing references to dtostrf() but where do I find the library for it so I can #include ?

    • Michael James on December 10, 2023 at 6:35 pm

      I believe it’s part of the standard library, so I do not think you need to include anything. Which board are you using?

  7. KR_edn on December 20, 2023 at 10:19 am

    Hello,
    I think this is something I need, but I don’t know how to use it. I would just like the BMP280 temperature sensor to display the value on the LCD with one decimal place instead of two, as it is now. I use the u8g2 library and the command: //u8g2.println(bmp.readTemperature());//
    I would like to use bmp.readTemperature as FLOAT.
    I tried //dtostrf// and it doesn’t work for me either way.
    I’m not a programmer, just an electronics enthusiast. Have You any idea?
    Thanks in advance.

    • Michael James on December 20, 2023 at 11:14 am

      Great questions! Could you paste the line of dtostrf() you used?

  8. KR_edn on December 20, 2023 at 12:53 pm

    Thanks for reply.
    This is part of my code:

    char str[15];
    float f = bmp.readTemperature();
    dtostrf(f, 5, 1, str);
    u8g2.println (str);

  9. KR_edn on December 21, 2023 at 4:40 am

    In the meantime, they helped me at the local forum, where in addition to electronics there are also many programmers. The dtostrf command is not used, but perhaps someone else is helped by this solution:
    //
    char str[15];
    float x = bmp.readTemperature();
    int a,b;
    a=(int)x;
    b=(int)((x-a)*10);
    sprintf(str,”%d.%d”,a,b);
    u8g2.print(str);
    //

    But I will also be glad to solve with the “dtostrf” command , so maybe I can use it somewhere else.
    Thank you

  10. Ashley on February 4, 2026 at 12:30 pm

    Perfect – found you at last! I have been trying to send a serial string concatenation of temperature, pressure and humidity (from a BME280 sensor) with semi-colon delimiters from an Arduino through Bluetooth to an Android handset with my own MIT App Inventor app on it. I have had problems constructing such a string but sprintf and dtostrf seem to be the answer

Leave a Comment