• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Problem with dtostrf

Q&A Forum › Category: ESP32 › Problem with dtostrf
1 Vote Up Vote Down
Hervé Tavernier asked 6 years ago

In the “Esp32 MQTT – Publish an subscribe with Arduino IDE” project the float BME sensorreadings are converted to a char aray so they can be published as esp/reading topics.
For this conversion dtostrf(reading,1,2 readingString) is used.
Can you please explain the parameter 1 in this expression?
Thank you very much.
i am an absolute ESP32 – MQTT – beginner.
Hervé Tavernier Belgium

3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi Herve.
Here is the syntax of the stostrf() function

dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);

where

  • floatvar – float variable
  • StringLengthIncDecimalPoint – this is the minimum length of the string that will be created including the point
  • numVarsAfterDecimal: the number of digits after the decimal point
  • charbuf: the array to store the results

I think you may be confused because the length is 1, which is smaller than the number of decimal points.

1 is the minimal length, that means that it will work even if the string is bigger than that.

For example, if you define a minimum length of 8, you’ll get 8 characters, even if your string is smaller than that. It will add white spaces to make it 8 characters long. So, if you set it to 1, you guarantee that you won’t get white spaces.

I hope this is clear.

Regards,
Sara

0 Vote Up Vote Down
Hervé Tavernier answered 6 years ago

Hello Sara
 
I am very happy with your quick and clear answer to my question.
The parameter 1 now has no more secrets for me!
Thank you very much!
Hervé
P.S. Apologize for my poor English. My native language is Dutch

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Great.
No need to apologize. My native language is Portuguese. My English is not perfect either.
I’ll close this question. 
If you need further help you just need to open a new question in our forum.
Regards,
Sara
 

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.