Integer to String with decimal question

Hard to come up with a Subject that will make sense...

Since sprintf() can't accept floats, I found this bit of code to convert an integer to a String and add the decimal. However I'm confused on how to actually use it. I'm still learning about functions and pointers, so the inputs to this function I don't understand.

Thread in question:
https://forum.arduino.cc/index.php?topic=44262.0

Say my code was like this:

float tempFloat = 123.45; // Test float value
int floatToInt;
char Output[10];  // for ftoa function
char buffer1[21]; // Needs to be 21 for LCD line width + null (LCD stuff truncated for example simplicity)

void setup() {}

void loop() {
floatToInt = (tempFloat * 100);          // Convert float to int
Output = ftoa(???, ???, 2);              // This is what I'm struggling with, inputs to the function
sprintf(buffer1, "T:%2sF", Output);      // Format output buffer
Serial.println(buffer1);                 // Print output buffer


char *ftoa(char *a, double f, int precision) {
 long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
 
 char *ret = a;
 long heiltal = (long)f;
 itoa(heiltal, a, 10);
 while (*a != '\0') a++;
 *a++ = '.';
 long desimal = abs((long)((f - heiltal) * p[precision]));
 itoa(desimal, a, 10);
 return ret;
}

Since sprintf() can't accept floats

...use dtostrf

TheMemberFormerlyKnownAsAWOL:
...use dtostrf

That is how I'm currently doing it.
I'm trying to "append" strings so I can print multiple variables on the same line. This is the way I thought I could get it working, but I keep having issues. I'm not sure if the dtostrf is the cause or improper array lengths or what.

In the example below:

"line1" is no longer printing to screen, "line2b" is not printing either. "line3a" and "line3b" are printing normally.

#include <LiquidCrystal.h>
const int rs = 22, en = 23, d4 = 24, d5 = 25, d6 = 26, d7 = 27;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Delay
  unsigned long now = millis();
  unsigned long lastMeasure = 0;

// Fixed values for example - taken from sensor readings
  float coffeeBoilerTemp = 195.50;
  float serviceBoilerTemp = 245.60;
  float coffeePressure = 10.15;
  float boilerPressure = 1.45;

// LCD Buffer
  char line0[21];
  char line1[21];
  char line2a[10];
  char line2b[11];
  char line3a[10];
  char line3b[11];

void setup() {
  lcd.begin(20, 4);
}
void loop() {
  now = millis();
    if (now - lastMeasure > 1000) {  // Update LCD every second
    lastMeasure = now;
    char coffeeTempFloat[7];
    char boilerTempFloat[6];
    char coffeePressureFloat[7];
    char boilerPressureFloat[6];
    dtostrf(coffeeBoilerTemp,6,2,coffeeTempFloat);
    dtostrf(serviceBoilerTemp,6,2,boilerTempFloat);
    dtostrf(coffeePressure,5,2,coffeePressureFloat);
    dtostrf(boilerPressure,5,2,boilerPressureFloat);
    sprintf(line0, "___[Title Screen]___");
    sprintf(line1, "       Ready");
    sprintf(line2a, "G%7sF   ", coffeeTempFloat);
    sprintf(line2b, "S%6sF", boilerTempFloat);
    sprintf(line3a, "G%7sb   ", coffeePressureFloat);
    sprintf(line3b, "S%6sb", boilerPressureFloat);
    refreshLCD();
  }
}

// Write LCD Buffer to Screen
void refreshLCD() {
  lcd.setCursor(0, 0);
  lcd.print(line0);
  lcd.setCursor(0, 1);
  lcd.print(line1);
  lcd.setCursor(0, 2);
  lcd.print(line2a);
  lcd.print(line2b);
  lcd.setCursor(0, 3);
  lcd.print(line3a);
  lcd.print(line3b);
}

'boilerTempFloat' (terrible name, btw, it is a char array not a float) is 6 chars.
Your call to dtostrf() specifies a min width of 6 so there is no room left for the trailing null char

blh64:
'boilerTempFloat' (terrible name, btw, it is a char array not a float) is 6 chars.
Your call to dtostrf() specifies a min width of 6 so there is no room left for the trailing null char

Fixed terrible variable names :slight_smile:

Code below, increased array size to 8 for each value, same output no change.

#include <LiquidCrystal.h>
const int rs = 22, en = 23, d4 = 24, d5 = 25, d6 = 26, d7 = 27;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Delay
  unsigned long now = millis();
  unsigned long lastMeasure =0;

// Fixed values for example - taken from sensor readings
  float coffeeBoilerTemp = 195.50;
  float serviceBoilerTemp = 245.60;
  float coffeePressure = 10.15;
  float boilerPressure = 1.45;

// LCD Buffer
  char line0[21];
  char line1[21];
  char line2a[10];
  char line2b[11];
  char line3a[10];
  char line3b[11];

void setup() {
  lcd.begin(20, 4);
}
void loop() {
  now = millis();
    if (now - lastMeasure > 1000) {  // Update LCD every second
    lastMeasure = now;
    char coffeeTempOut[8];
    char boilerTempOut[8];
    char coffeePressureOut[8];
    char boilerPressureOut[8];
    dtostrf(coffeeBoilerTemp,6,2,coffeeTempOut);
    dtostrf(serviceBoilerTemp,6,2,boilerTempOut);
    dtostrf(coffeePressure,5,2,coffeePressureOut);
    dtostrf(boilerPressure,5,2,boilerPressureOut);
    sprintf(line0, "___[Title Screen]___");
    sprintf(line1, "       Ready");
    sprintf(line2a, "G%7sF   ", coffeeTempOut);
    sprintf(line2b, "S%6sF", boilerTempOut);
    sprintf(line3a, "G%7sb   ", coffeePressureOut);
    sprintf(line3b, "S%6sb", boilerPressureOut);
    refreshLCD();
  }
}

// Write LCD Buffer to Screen
void refreshLCD() {
  lcd.setCursor(0, 0);
  lcd.print(line0);
  lcd.setCursor(0, 1);
  lcd.print(line1);
  lcd.setCursor(0, 2);
  lcd.print(line2a);
  lcd.print(line2b);
  lcd.setCursor(0, 3);
  lcd.print(line3a);
  lcd.print(line3b);
}

Why don't you simply print out to the serial monitor all your variables that you are using so you can see what they contain?

#include <LiquidCrystal.h>
const int rs = 22, en = 23, d4 = 24, d5 = 25, d6 = 26, d7 = 27;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Delay
  unsigned long now = millis();
  unsigned long lastMeasure =0;

// Fixed values for example - taken from sensor readings
  float coffeeBoilerTemp = 195.50;
  float serviceBoilerTemp = 245.60;
  float coffeePressure = 10.15;
  float boilerPressure = 1.45;

// LCD Buffer
  char line0[21];
  char line1[21];
  char line2a[10];
  char line2b[11];
  char line3a[10];
  char line3b[11];

void setup() {
  lcd.begin(20, 4);
  Serial.begin(9600);
}
void loop() {
  now = millis();
    if (now - lastMeasure > 1000) {  // Update LCD every second
    lastMeasure = now;
    char coffeeTempOut[8];
    char boilerTempOut[8];
    char coffeePressureOut[8];
    char boilerPressureOut[8];
    dtostrf(coffeeBoilerTemp,6,2,coffeeTempOut);
    dtostrf(serviceBoilerTemp,6,2,boilerTempOut);
    dtostrf(coffeePressure,5,2,coffeePressureOut);
    dtostrf(boilerPressure,5,2,boilerPressureOut);
    sprintf(line0, "___[Title Screen]___");
    sprintf(line1, "       Ready");
    sprintf(line2a, "G%7sF   ", coffeeTempOut);
    sprintf(line2b, "S%6sF", boilerTempOut);
    sprintf(line3a, "G%7sb   ", coffeePressureOut);
    sprintf(line3b, "S%6sb", boilerPressureOut);
    Serial.print("coffeeTempOut = '"); Serial.print(coffeeTempOut); Serial.println("'");
    Serial.print("boilerTempOut = '"); Serial.print(boilerTempOut); Serial.println("'");
    Serial.print("coffeePressureOut = '"); Serial.print(coffeePressureOut); Serial.println("'");
    Serial.print("boilerPressureOut = '"); Serial.print(boilerPressureOut); Serial.println("'");

    Serial.print("line0 = '"); Serial.print(line0); Serial.print("', "); Serial.println(strlen(line0));
    Serial.print("line1 = '"); Serial.print(line1); Serial.print("', "); Serial.println(strlen(line1));
    Serial.print("line2a = '"); Serial.print(line2a); Serial.print("', "); Serial.println(strlen(line2a));
    Serial.print("line2b = '"); Serial.print(line2b); Serial.print("', "); Serial.println(strlen(line2b));
    Serial.print("line3a = '"); Serial.print(line3a); Serial.print("', "); Serial.println(strlen(line3a));
    Serial.print("line3b = '"); Serial.print(line3b); Serial.print("', "); Serial.println(strlen(line3b));
    
    refreshLCD();

    while(1); // don't need to loop
  }
}

// Write LCD Buffer to Screen
void refreshLCD() {
  lcd.setCursor(0, 0);
  lcd.print(line0);
  lcd.setCursor(0, 1);
  lcd.print(line1);
  lcd.setCursor(0, 2);
  lcd.print(line2a);
  lcd.print(line2b);
  lcd.setCursor(0, 3);
  lcd.print(line3a);
  lcd.print(line3b);
}