Formating a string (round float) to string

**Using a string to display temperature on an OLED

Hello

I just dont get it on how to properly to format a string. The goal, print temperature and humidity using a fix 3 characters spaces. I did look at Sprintf, but I dont get it.

some info:

  • The string is limited to 10 characters per line
  • Each line is 3 characters for actual temperature + 3 spaces + 3 characters for the target temperature + ("C" or "F")
    Each data on a line be always aligned. If lower than 100, space to replace missing characters
  • Temperature value for sensor is a float
  • If value is lower than 100, meaning using less than 3 characters, missing number replaced by a space

Any help appreciated
Martin

image

What don't you get about sprintf()? There are countless tutorials on line.
Example

For help with a specific problem in your code, please read and follow the directions in the "How to get the best out of this forum" post, linked at the head of every forum category.

Which arduino are you using? On most, sprintf does not supports floats.

Easiest approach is to round the float and save as an integer, then use the integer with sprintf.

You can find a lot more information if you look for references for printf(), such as https://cplusplus.com/reference/cstdio/printf/

3 digits is not enough for a floating point temperature if you account for the dot and even just one decimal and a possible negative sign.

What’s the real range of temperature, can it be -25.5°C ?

And you probably should update the screen with the right granularity and prevent unwanted blinking if you repaint the whole screen by addressing each field separately. get the value in a String without leading spaces if this is what you want and then ask the length of that String and pas the start of the string with as many spaces needed to match the field width.

If you are on AVR then sprintf by default does not handle floating point variables but if you display integers only then indeed convert the variable to an integer before

float tempC = -25.4;
…
int displayTempC = tempC;
// then use displayTempC in sprintf 

sprintf() on Arduino doesn't support floats. dtostrf() can be used to format c-strings of floats that are formatted into longer strings with multiple values

22.5C 72.8C
void
formatTemps (
    float a,
    float b )
{
    char s [20];
    char p [10];
    char q [10];

    dtostrf (a, 4, 1, p);
    dtostrf (b, 4, 1, q);
    sprintf (s, "%sC %sC", p, q);
    Serial.println (s);
}

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

    formatTemps (22.5, 72.8);
}

void loop ()
{
}

Sorry for a late reply, just came back from work. Thanks to all, will look at your proposition this weekend. To tired for now.

The project is to build a general purpose temperature and humidity controller with differents type of probes input for

  • A charcoal BBQ

  • A smoker

  • Grow mushroom

  • Aging cheese

  • I will not need a decimal precision

  • Temperature will never be in a negative range

Martin

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.