By Mistergreen


2014-12-25 21:54:07 8 Comments

I have this arduino sketch,

char temperature[10];
float temp = 10.55;
sprintf(temperature,"%f F", temp);
Serial.println(temperature);

temperature prints out as

? F

Any thoughts on how to format this float? I need it to be a char string.

4 comments

@Excalibur 2020-07-08 20:50:12

I've struggled for a few hours on getting this right, but I did finally. And this uses modern Espressif C++ provided by Platformio, and my target MCU is an ESP32.

I wanted to display a prefix label, the float/int value, then the unit, all inline.

I can't relay on seperate Serial.print() statements, as I am using an OLED display.

Here's my code example:

  int strLenLight = sizeof("Light ADC: 0000");
  int strLenTemp = sizeof("Temp: 000.0 °C");
  int strLenHumd = sizeof("Humd: 00.0 %");

  char displayLight[strLenLight] = "Light ADC: ";
  char displayTemp[strLenTemp] = "Temp: ";
  char displayHumd[strLenHumd] = "Humd: ";

  snprintf(strchr(displayLight, '\0'), sizeof(displayLight), "%d", light_value);
  snprintf(strchr(displayTemp, '\0'), sizeof(displayTemp), "%.1f °C", temperature); 
  snprintf(strchr(displayHumd, '\0'), sizeof(displayHumd), "%.1f %%", humidity); 

  Serial.println(displayLight);
  Serial.println(displayTemp);
  Serial.println(displayHumd);

Which displays:

Light ADC: 1777
Temp: 25.4 °C
Humd: 55.0 %

@Helmut Wunder 2020-02-18 13:27:10

dtostrf() is deprecated, and it doesn't exist on every board core platforms. On the other hand, sprintf() doesn't format floats on AVR platforms!

@Gerhard 2020-02-18 14:29:11

The sprintf() linked by default does not support %f, but you can link in a full version of sprintf() if required.

@Cameron Lowell Palmer 2017-07-31 18:49:53

As has been stated before Float support is not included in sprintf on Arduino.

String class

Arduino has its own String class.

String value = String(3.14);

then,

char *result = value.c_str();

String class reference, link above

Constructs an instance of the String class. There are multiple versions that construct Strings from different data types (i.e. format them as sequences of characters), including:

  • a constant string of characters, in double quotes (i.e. a char array)
  • a single constant character, in single quotes
  • another instance of the String object
  • a constant integer or long integer
  • a constant integer or long integer, using a specified base
  • an integer or long integer variable
  • an integer or long integer variable, using a specified base
  • a float or double, using a specified decimal palces

@JrBenito 2017-08-31 03:54:15

Important notice to the pointer above: "When you modify the String object, or when it is destroyed, any pointer previously returned by c_str() becomes invalid and should not be used any longer."

@Cameron Lowell Palmer 2017-08-31 08:05:25

I would say, obviously, but yes, that is an excellent point for people new to Arduino programming.

@Cerin 2018-03-29 01:19:18

Arduino's String class does not have a method that accepts float or double. This only works because it converts it to an integer.

@Cerin 2018-03-31 18:03:43

@CameronLowellPalmer You're right, my mistake. The most recent Arduino release does include String support for double and float. I'm using an older release included with Ubuntu that pre-dates this, and throws a compiler error when I try to pass a float to String().

@TechNyquist 2019-04-12 17:31:23

Caution: just a tedious yet important note about String class for newbies: it dynamically allocates/deallocates heap memory, because of this it is a great generator of "heap-fragmentation" in the long run. Try to keep its use limited preferring C-style strings. More about Arduino memory caveats

@Orkhan Alikhanov 2019-08-17 12:35:13

It even takes second parameter as precision, String(3.1434, 4)

@Dinal24 2014-12-26 01:22:09

Due to some performance reasons %f is not included in the Arduino's implementation of sprintf(). A better option would be to use dtostrf() - you convert the floating point value to a C-style string, Method signature looks like:

char *dtostrf(double val, signed char width, unsigned char prec, char *s)

Use this method to convert it to a C-Style string and then use sprintf, eg:

char str_temp[6];

/* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
dtostrf(temp, 4, 2, str_temp);
sprintf(temperature,"%s F", str_temp);

You can change the minimum width and precision to match the float you are converting.

@Fahad Siddiqui 2016-03-04 09:51:07

Worked fine in my case. Thumbs up!

@Paulus 2018-06-15 13:20:08

just a caution, the width of your buffer: str_temp is very tight at 6 characters

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] How can I convert a std::string to int?

  • 2011-10-05 15:23:45
  • Brandon
  • 1416166 View
  • 520 Score
  • 19 Answer
  • Tags:   c++ string int

40 Answered Questions

[SOLVED] std::string formatting like sprintf

12 Answered Questions

[SOLVED] How can one print a size_t variable portably using the printf family?

  • 2010-03-26 15:52:25
  • Arun
  • 296504 View
  • 416 Score
  • 12 Answer
  • Tags:   c printf

10 Answered Questions

[SOLVED] Printing leading 0's in C?

  • 2008-09-30 16:34:43
  • zxcv
  • 372192 View
  • 355 Score
  • 10 Answer
  • Tags:   c printf

3 Answered Questions

[SOLVED] Converting from string to char* on Arduino

  • 2014-07-29 12:35:06
  • Fiona Zhang
  • 1008 View
  • 0 Score
  • 3 Answer
  • Tags:   c++ arduino

7 Answered Questions

[SOLVED] How to pass variable number of arguments to printf/sprintf

1 Answered Questions

arduino serial and pySerial

4 Answered Questions

[SOLVED] Arduino: printf/fprintf prints question mark instead of float

3 Answered Questions

Sending float type data from Arduino to Python

Sponsored Content